next up previous contents
Next: The USE Renames Facility Up: Modules - Procedure Declaration Previous: Encapsulation Example

 

Encapsulation - Stack example

We can also encapsulate the stack program,

    MODULE stack
     IMPLICIT NONE
     INTEGER, PARAMETER :: stack_size = 100
     INTEGER, SAVE :: store(stack_size), pos = 0
    CONTAINS
     SUBROUTINE push(i)
      INTEGER, INTENT(IN) :: i
      IF (pos < stack_size) THEN
       pos = pos + 1; store(pos) = i
      ELSE
       STOP 'Stack Full error'
      END IF
     END SUBROUTINE push
     SUBROUTINE pop(i)
      INTEGER, INTENT(OUT) :: i
      IF (pos > 0) THEN
       i = store(pos); pos = pos - 1
      ELSE
       STOP 'Stack Empty error'
      END IF
     END SUBROUTINE pop
    END MODULE stack

all aspects of the stack are now defined in one place -- the module provides all declarations, access functions and storage to implement a simple integer stack.

Any program unit that includes the line:

    USE stack
can access pop and push therefore use the stack.

Return to corresponding overview page gif


next up previous contents
Next: The USE Renames Facility Up: Modules - Procedure Declaration Previous: Encapsulation Example

©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997
Not for commercial use.