FORTRAN Reference Manual
Program Units
FORTRAN Reference Manual—528615-001
4-5
Function Subprograms
When the function reference executes, FORTRAN evaluates any actual arguments
that are expressions, and associates the actual arguments with the corresponding
dummy arguments in the FUNCTION statement. Control then passes to the
subprogram. The function subprogram must contain the following statements:
•
A FUNCTION statement as its first statement
•
A statement establishing a value for the function name
•
An END statement as its last statement
The function subprogram cannot contain a PROGRAM, SUBROUTINE, BLOCK DATA,
or another FUNCTION statement.
Write the FUNCTION statement in the following form:
FUNCTION function-name ( dummy-argument-list)
FORTRAN determines the type of a function name in the same way as it determines
the type of any symbolic name. You can place a type declaration on the same line as
the FUNCTION statement as in the following example:
CHARACTER*20 FUNCTION employee (dept, empnumber)
Or you can declare its type with a separate declaration statement; for example:
FUNCTION employee (dept, empnumber)
CHARACTER employee*20
If you use a FUNCTION statement to declare a function’s type, you cannot declare it
again using a type-declaration statement in the same program unit. Note, however, that
you must also declare the function’s type in each calling program unit, unless it is typed
by default.
Figure 4-1. Function Subprogram and Calling Program
function reference
actual arguments
function name
dummy arguments
correspond to
used in
function name must be defined
within function subprogram
VST0401.vsd
PROGRAM main
.
.
READ(5,*) a, b, c
.
cost = rate * metric(a, b, c)
.
.
END
FUNCTION metric (x, y, z)
.
.
.
.
metric = x * y * z
.
.
.
END










