next up previous contents
Next: Encapsulation - Stack example Up: Modules - Procedure Declaration Previous: Modules - Procedure Declaration

 

Encapsulation Example

The following program has the subroutines sub1 and sub2 encapsulated into the module,

    MODULE related_procedures
     ! INTERFACEs of MODULE PROCEDURES do 
     ! not need to be specified they are 
     ! 'already present'
    CONTAINS
     SUBROUTINE sub1(A,B,C)
      ! Can see Sub2's INTERFACE
      ...
     END SUBROUTINE sub1
     SUBROUTINE sub2(time,dist)
      ! Can see Sub1's INTERFACE
      ...
     END SUBROUTINE sub2
    END MODULE related_procedures

    PROGRAM use_of_module
     USE related_procedures
     CALL sub1((/1.0,3.141,0.57112/),2,'Yobot')
     CALL sub2(t,d)
    END PROGRAM use_of_module

the interfaces of all module procedures are visible to all other modules procedures in the same module, this means, sub1 can call sub2 or vice versa and there is no need for any additional INTERFACE declarations to be typed in.

Consider the following erroneous code,

    MODULE Schmodule
     CONTAINS
      LOGICAL FUNCTION bool(y,z)
       INTEGER, INTENT(IN) :: y, z
       bool = .FALSE.
       IF (y > z) bool = .TRUE.
      END FUNCTION bool
      SUBROUTINE subby(x,y,z)
       INTEGER, INTENT(IN) :: y, z 
       INTEGER, INTENT(OUT) :: x
! next line is error
       LOGICAL :: bool
!
       if ( bool(y,z) ) x = 1
      END SUBROUTINE subby
    END MODULE Schmodule

Both procedures are visible to one another by host association so in subby the intention of declaring the other module procedure is misguided. The LOGICAL FUNCTION bool is already visible to subby but due to the declaration is superseded by a local LOGICAL object called bool. When the module is compiled a complaint is made that the external function bool is missing.

Now try this question gif

Now try this question gif

Now try this question gif

Return to corresponding overview page gif


next up previous contents
Next: Encapsulation - Stack example Up: Modules - Procedure Declaration Previous: Modules - Procedure Declaration

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