6.2.1 Statement Functions

A statement function is a computing procedure defined in the same program unit in which it is referenced. It is defined by a single statement that is similar in form to an assignment statement. A computation is performed each time you refer to the statement function, and the resulting value is then made available to the expression that contains the statement function reference.

Statement Function Definitions

A statement function definition takes the following form:

fun ([p [,p] . . . ]) = e
fun
Is the symbolic name of the statement function.
p
Is a dummy argument.
e
Is an arithmetic, logical, or character expression that defines the computation to be performed.

Statement Function References

A statement function reference takes the following form:

f ([a [,a] . . . ])
f
Is the symbolic name of the statement function.
a
Is an actual argument.

Rules and Behavior

When a statement function reference appears in an expression, the values of the actual arguments are associated with the dummy arguments in the statement function definition. The expression in the definition is then evaluated. The resulting value is used to complete the evaluation of the expression containing the function reference.

The data type of a statement function is determined either implicitly by the initial letter of the function name, or explicitly in a type declaration statement. Any data type is valid, including the character data type.

Dummy arguments in a statement function indicate only the number, order, and data type of the actual arguments. You can use the names of dummy arguments to represent other entities in the program unit.

Except for the data type, declarative information associated with an entity is not associated with dummy arguments in the statement function; declaring an entity to be an array or to be in a common block does not affect a dummy argument with the same name.

Actual arguments must agree in number, order, and data type with their corresponding dummy arguments.

The name of the statement function cannot represent any other entity within the same program unit.

The expression in a statement function definition can contain function references. If a reference to another statement function appears in the expression, it must be previously defined in the same program unit.

Any reference to a statement function must appear in the same program unit as the definition of that function.

A statement function reference must appear as (or be part of) an expression. The reference cannot appear on the left side of an assignment statement.

Examples

The following statement function definitions are valid:

VOLUME(RADIUS) = 4.189*RADIUS**3
SINH(X) = (EXP(X)-EXP(-X))*0.5

CHARACTER*10 CSF,A,B
CSF(A,B) = A(6:10)//B(1:5)

The following statement function definition is invalid because it contains a constant, which cannot be used as a dummy argument:

AVG(A,B,C,3.) = (A+B+C)/3.

Consider the following definition:

AVG(A,B,C) = (A+B+C)/3.

The following references, based on the previous statement, are valid:

GRADE = AVG(TEST1,TEST2,XLAB)
IF (AVG(P,D,Q) .LT. AVG(X,Y,Z)) GO TO 300

The following reference is invalid because the data type of the third argument does not agree with the dummy argument:

FINAL = AVG(TEST3,TEST4,LAB2)


Previous Page Next Page Table of Contents