# Statement Function

Statement: Defines a function in a single statement in the same program unit in which the procedure is referenced.

fun([d-arg [,d-arg]...]) = expr

fun | Is the name of the statement function.
d-arg | Is a dummy argument. A dummy argument can appear only once in any list of dummy arguments, and its scope is local to the statement function.
expr | Is a scalar expression defining the computation to be performed. Named constants and variables used in the expression must have been declared previously in the specification part of the scoping unit or made accessible by use or host association. If the expression contains a function or statement function reference, that function must have been defined previously as a function or statement function in the same program unit. A statement function reference takes the following form: fun([ a-arg[, a-arg] ...])
fun | Is the name of the statement function.
a-arg | Is an actual argument.

## Description

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 can be explicitly defined in a type declaration statement. If no type is specified, the type is determined by implicit typing rules in effect for the program unit.

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

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

The name of the statement function cannot be the same as the name of any other entity within 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.

A statement function must not be provided as a procedure argument.

A statement function is PURE if the only functions it references are PURE and it does not reference a data object with the VOLATILE attribute.

## Example

The following are examples of statement functions:

      REAL VOLUME, RADIUS
      VOLUME(RADIUS) = 4.189*RADIUS**3

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

The following example shows a statement function and some references to it:

      AVG(A,B,C) = (A+B+C)/3.
      ...
      GRADE = AVG(TEST1,TEST2,XLAB)
      IF (AVG(P,D,Q) .LT. AVG(X,Y,Z)) STOP
      FINAL = AVG(TEST3,TEST4,LAB2)       ! Invalid reference; implicit
      ...                                 ! type of third argument does not
      ...                                 ! match implicit type of dummy argument

Implicit typing problems can be avoided if all arguments are explicitly typed.

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

      REAL COMP, C, D, E
      COMP(C,D,E,3.) = (C + D - E)/3.

The following shows another example:

      Add (a, b) = a + b
      REAL(4) y, x(6)
      . . .
      DO n = 2, 6
        x(n) = Add (y, x(n-1))
      END DO

Parent topic: S

## See Also

FUNCTION
Argument Association
Use and Host Association