Statement Functions

A statement function is a user-defined function which is defined and used within a program unit. It appears after the type declarations but before the executable statements, and looks superficially like a simple assignment statement.

fname(dummy1, dummy2, …, dummyn) = expression

A statement function may contain any number of dummy arguments, all of which must appear in the expression on the right hand side of the statement. The expression also may contain constants, variables and array elements defined elsewhere in the program unit in which the statement function appears. Intrinsic functions, external functions and previously defined statement functions also may appear in the definition of a statement function.

Statement functions may return any of the six FORTRAN 77 data types. fname is a symbolic name and must be unique within the program unit defining it. It conforms to normal FORTRAN 77 implicit typing. To override this, simply declare the name fname in the type declarations. If the statement function returns a CHARACTER value, then the length must be declared in the statement function definition.

The dummy arguments dummy1, dummy2, …, dummyn are also symbolic names conforming to normal FORTRAN 77 implicit typing. Their type(s) may be declared in the usual way. Each of these symbolic names must be unique; that is, the same dummy argument cannot appear twice in the list. However, the symbolic names of the dummy arguments may be used elsewhere in the program unit as variables of the same type. Use elsewhere in this manner does not affect the statement function.

Like an intrinsic or external function, a statement function is invoked by its name and argument list.

Example

      REAL C,F,TEMP
      C(F) = 5.0*(F - 32.0)/9.0
      WRITE(*,*)'Enter the temperature in Fahrenheit'
      READ(*,*)TEMP
      WRITE(*,*)'Fahrenheit = ',TEMP,' and Celsius = ',C(TEMP)

This statement function converts Fahrenheit to Celsius. Note that the dummy argument F appears in the right hand side of the statement 5.0*(F - 32.0)/9.0. The function is invoked by C(TEMP) in the second WRITE statement.

Example

In this program fragment, the statement function is invoked in an assignment statement with an array element as its actual argument. Notice also that the statement function uses the value of the constant C which is not an argument of the function.

      DOUBLE PRECISION C,E,M
      INTEGER          I,NMAX
      PARAMETER(C=299792458D0,NMAX=100)
      DOUBLE PRECISION ENERGY(NMAX),MASS(NMAX)
      E(M) = M*C**2
…
      DO 10, I = 1,NMAX
         ENERGY(I) = E(MASS(I))
   10 CONTINUE

Example

      DOUBLE PRECISION F,FORCE,G,M1,M2,R
      PARAMETER(G=6.67428D-11)
      F(M1,M2,R) = G*M1*M2/R**2
…
      FORCE = F(MASS,MASS,RADIUS)

This definition of a statement function calculating the attractive force between two bodies involves three arguments. Although the dummy arguments must be unique, the actual arguments do not have the same restriction. In this example, the force between two identical bodies (same mass) is determined.