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 stackcan access pop and push therefore use the stack.