next up previous contents
Next: Procedure Interfaces Up: External Function Syntax Previous: External Function Syntax

 

Function Example

A function is invoked by its appearance in an expression at the place where its result value is needed,

    total = total + largest(a,b,c)

The function is defined as follows,

    INTEGER FUNCTION largest(i,j,k)
     INTEGER :: i, j, k
     largest = i
     IF (j .GT. largest) largest = j
     IF (k .GT. largest) largest = k
    END FUNCTION largest

or equivalently as,

    FUNCTION largest(i,j,k)
     INTEGER :: i, j, k
     INTEGER :: largest
      ...
    END FUNCTION largest

The largest value of i, j and k will be substituted at the place where the function call is made. As with subroutines, the dummy and actual arguments must match in type kind and rank.

More than one function (or the same function more than once) may be invoked in a single statement. For example,

    rezzy = funky1(a,b,c) + funky2(a,b,c)

Care must be taken that the order of execution will not alter the result; aside from operator precedence, the order of evaluation of a statement in a Fortran 90 program is undefined. It is not specified whether funky1 or funky2 is evaluated first; they could even be executed in parallel!

If a function invocation has side-effects and it is called twice in the same statement then problems may occur. To safeguard against this a function invocation should not assign to its arguments, modify any global variables or perform I/O! A function which obeys these restrictions is termed PUREgif

Return to corresponding overview page gif


next up previous contents
Next: Procedure Interfaces Up: External Function Syntax Previous: External Function Syntax

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