### 6.2.2.3 Function Reference

A function reference that transfers control to a function subprogram takes the following form:

    nam ([a [,a] . . . ])

nam
  Is the symbolic name of the function.
a
  Is an actual argument.

When control transfers to a function subprogram, the values of any actual arguments in the function reference are associated with any dummy arguments in the FUNCTION statement. The statements in the subprogram are executed, the resulting value is assigned to the name of the function, and control returns to the calling program unit.

The program unit uses the value assigned to the function name to complete the evaluation of the expression containing the function reference.

The data type of a function name can be specified explicitly in the FUNCTION statement or in a type declaration statement; it can also be specified implicitly. The function name defined in the function subprogram must have the same data type as the function name in the calling program unit.

The FUNCTION statement must be the first statement of a function subprogram, unless an OPTIONS statement is specified. A function subprogram cannot contain a SUBROUTINE statement, a BLOCK DATA statement, a PROGRAM statement, or another FUNCTION statement. ENTRY statements can be included to provide multiple entry points to the subprogram (see Section 6.2.4).

Examples

Consider the following example:

          FUNCTION ROOT(A)

          X  = 1.0

    2     EX = EXP(X)

          EMINX = 1./EX

          ROOT  = X - ((EX+EMINX)*.5+COS(X)-A)/((EX-EMINX)*.5-SIN(X))

          IF (ABS(X-ROOT) .LT. 1E-6) RETURN

          X  = ROOT

          GO TO 2

          END

To obtain the root of the function, the previous example uses the Newton-Raphson iteration method:

                         F(X) = cosh(X)+cos(X)-A = 0

The value of A is passed as an argument. The iteration formula for this root is as follows:

                            cosh(Xi) + cos(Xi) - A

                Xi+1 = Xi - ------------------------

                              sinh(Xi) - sin(Xi)

This formula is calculated repeatedly until the difference between X(i) and X(i+1) is less than 1.0E-6. The function uses the Fortran intrinsic functions EXP, SIN, COS, and ABS (see Section 6.3).

The next example is a passed-length character function. It returns the value of its argument, repeated to fill the length of the function.

         CHARACTER*(*) FUNCTION REPEAT(CARG)

         CHARACTER*1 CARG

         DO 10 I=1,LEN(REPEAT)

    10   REPEAT(I:I) = CARG

         RETURN

         END

Within any given program unit all references to a passed-length character function must have the same length. In the following example, the REPEAT function has a length of 1000:

    CHARACTER*1000 REPEAT, MANYAS, MANYZS

    MANYAS = REPEAT('A')

    MANYZS = REPEAT('Z')

Another program unit within the executable program can specify a different length. In the following example, the REPEAT function has a length of 2:

    CHARACTER HOLD*6, REPEAT*2

    HOLD = REPEAT('A')//REPEAT('B')//REPEAT('C')

Note
  A function name identifier refers to the return value in a function (not the function itself), unless an argument list is present. Therefore, it is not possible to pass a function as an argument to another routine from inside the function. For example, consider the following:

      INTEGER FUNCTION RECURSIVE_FUNCTION

       . . .

      CALL OTHERSUB (RECURSIVE_FUNCTION)

  The reference to RECURSIVE_FUNCTION in the CALL statement passes the function return value, not the function itself.

Previous Page Next Page Table of Contents