next up previous contents
Next: Example of Scoping Issues Up: Scope Previous: Local Data

 

Host Association

In FORTRAN 77, different routines were entirely separate from each other, they did not have access to each others variable space and could only communicate through argument lists or by global storage (COMMON); such procedures are known as external.

Procedures in Fortran 90 may contain internal procedures which are only visible within the program unit in which they are declared, in other words they have a local scope. Consider the following example,

    SUBROUTINE calculate_pay
     IMPLICIT NONE
     REAL :: pay, tax
     pay = ...
     tax = ...
     CALL print_pay
    CONTAINS
     SUBROUTINE print_pay
      PRINT*, pay,tax
      CALL change_pay
      PRINT*, pay,tax
     END SUBROUTINE print_pay
     SUBROUTINE change_pay
      pay = ...
      tax = ...
     END SUBROUTINE change_pay
    END SUBROUTINE calculate_pay

    SUBROUTINE hex
     IMPLICIT NONE
     REAL :: pay, tax
      ...
    END SUBROUTINE hex

print_pay and change_pay are both internal subroutines of calculate_pay. These internal procedures have access to the data environment of their host.

The variables pay and tax in the subroutine print_pay and change_pay refer to the entities with the same names which were defined in the host subroutine. print_pay and change_pay have access to all calculate_pays declarations by host association including use associated objects. change_pay cannot access any of the declarations of print_pay (for example, tax_paid,) and vice-versa. pay and tax in hex are unrelated to any object in calculate_pay.

print_pay could invoke other internal procedures which are contained by the same outer procedure, for example, print_pay can call change_pay and vice-versa (but cannot call calculate_pay because it is not recursive, see Section gif for discussion about recursion).

Now try this question gif


next up previous contents
Next: Example of Scoping Issues Up: Scope Previous: Local Data

Adam Marshall ©University of Liverpool, 1996
Fri Dec 6 15:03:35 GMT 1996
Not for commercial use.