next up previous contents
Next: External Subroutine Example Up: Subroutine Syntax Previous: Subroutine Syntax

 

Subroutine Example

A subroutine (either internal or external) such as sub1 in the following example is invoked by its appearance in a CALL statement,

    CALL sub1(arg1, arg2, arg3)

The variables (or expressions) denoted by arg1, arg2 and arg3 are known as actual arguments and, subject to certain conditions (see Section gif), must match in number with the arguments in the SUBROUTINE definition; they must also be of the same type, kind and rankgif.

An example of a subroutine definition:

  SUBROUTINE sub1(a,b,c)
   IMPLICIT NONE
   REAL :: a, b, c, s
   REAL :: tmp
     ...
    CALL sum_sq(a,b,c,s)
     ...
  CONTAINS
   SUBROUTINE sum_sq(aa,bb,cc,ss)
    REAL, INTENT(IN) :: aa, bb, cc
    REAL, INTENT(OUT) :: ss
     ss = aa*aa + bb*bb + cc*cc
   END SUBROUTINE sum_sq
  END SUBROUTINE sub1

The variables appearing in a SUBROUTINE statement, a, b and c, are known as dummy arguments, whereas variables appearing in a CALL statement, arg1, arg2 and arg3, are known as . actual arguments. A dummy argument is said to be associated with its corresponding actual argument; here a is associated with arg1, b with arg2 and so on. A reference to a dummy argument is really a reference to its corresponding actual argument, for example, changing the value of a dummy argument in reality changes the value of the actual argument.

The SUBROUTINE sub1 contains an internal procedure sum_sq which is invoked in the same way as an external procedure but will never need to be declared. The definition is the same as for a regular subroutine except that it follows a CONTAINS statement and is actually a part of the host procedure. This section of the program unit may contain any number of declarations of internal procedures (both functions and subroutines). An internal procedure cannot have a CONTAINS block.

The procedure sum_sq has full access to all the objects declared in the surrounding procedure, this means that the IMPLICIT NONE statement and all other declarations are visible in sum_sq by host-association. Any variables will have the values that they possessed at the exact point that the procedure was invoked. (The variables a, b, c and s would be visible in the procedure but it is good practice to pass them as arguments anyway -- this will make the procedure more flexible. a, b, c and s may be referred to in sum_sq but as they are argument associated with aa, bb, cc and ss, they may not be assigned to. If the value of a dummy argument is modified then so is the value of the corresponding actual argument, for example, if aa is modified the so is the value of a.)

The above subroutines are incomplete as there is no INTENT specified -- this has been deliberately left out and will be described later (see Section 4.1.11).


next up previous contents
Next: External Subroutine Example Up: Subroutine Syntax Previous: Subroutine Syntax

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