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

 

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,

    PROGRAM CalculatePay
     IMPLICIT NONE
     REAL :: Pay, Tax, Delta
     INTEGER :: NumberCalcsDone = 0
     Pay = ...;  Tax = ... ; Delta = ...
     CALL PrintPay(Pay,Tax)
     Tax = NewTax(Tax,Delta)
      ....
    CONTAINS
     SUBROUTINE PrintPay(Pay,Tax)
      REAL, INTENT(IN) :: Pay, Tax
      REAL :: TaxPaid
       TaxPaid = Pay * Tax
       PRINT*, TaxPaid
       NumberCalcsDone = NumberCalcsDone + 1
     END SUBROUTINE PrintPay
     REAL FUNCTION NewTax(Tax,Delta)
      REAL, INTENT(IN) :: Tax, Delta
       NewTax =  Tax + Delta*Tax
       NumberCalcsDone = NumberCalcsDone + 1
     END FUNCTION NewTax
    END PROGRAM CalculatePay

PrintPay is an internal subroutine of CalculatePay and has access to NumberCalcsDone. It can be thought of as a global variable. It is said to be available to the procedures by host association. The variables Pay and Tax, on the other hand, are passed as arguments to the procedures. There are therefore available by argument association.

The decision of whether to give visibility to an object by host association or by argument association is tricky -- there are no hard and fast rules. If, for example, Pay, Tax and Delta had not been passed to the procedures as arguments but had been made visible by host association instead, then there would be no discernible difference in the results that the program produces. Likewise, NumberCalcsDone could have been communicated to both procedures by argument association instead of by host association. In a sense, the method that is used will depend on personal taste or a specific `in-house' coding-style. Here, Pay, Tax and Delta are not used in every single procedure in the same way as NumberCalcsDone which acts in a more global way!

NewTax cannot access any of the local declarations of PrintPay (for example, TaxPaid,) and vice-versa. NewTax and PrintPay can be thought of a resting at the same scoping level whereas the containing program, CalculatePay is at an outer (higher) scoping level (see Figure 13.1).

PrintPay can invoke other internal procedures which are contained by the same outer program unit (but cannot call itself as it is not recursive, see Section 14.6 for discussion about recursion).

Upon return from PrintPay the value of NumberCalcsDone will have increased by one due to the last line of the procedure.

Now try this question gif

Return to corresponding overview page gif


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

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