HP Fortran Programmer's Reference (September 2007)

Program units and procedures
Statement functions
Chapter 7 169
Statement functions
If an evaluation of a function with a scalar value can be expressed in just one Fortran
assignment statement, such a definition can be included in the specification part of a main
program unit or subprogram. This definition is known as a
statement function
. It is local to
the scope in which it is defined. The syntax is:
function-name
(
dummy-argument-list
) =
scalar-expression
All dummy arguments must be scalars. All entities used in
scalar-expression
must have
been declared earlier in the specification part. A statement function can reference another
statement function that has already been declared. The name cannot be passed as a
procedure-name argument. A statement function has an explicit interface.
The following example, stmt_func.f90, is the same as the one listed in “Internal procedures”
on page 167 except that it implements get_avg as a statement function rather than as an
internal function. As noted in the comments to the program, the elements of the array x are
passed to the statement function as separate arguments because dummy arguments of a
statement function must be scalars.
Example 7-3 stmt_func.f90
PROGRAM main
! declare and initialize an array to pass to an external
! procedure
REAL, DIMENSION(3) :: values = (/2.0, 5.0, 7.0/)
! Because the dummy argument to print_avg is an assumed-shape
! array (see the definition of print_avg below), the
! procedure interface of print_avg must be made
! explicit within the calling program unit.
INTERFACE
SUBROUTINE print_avg(x)
REAL :: x(:)
END SUBROUTINE print_avg
END INTERFACE
CALL print_avg(values)
END PROGRAM main
! print_avg is an external subprogram
SUBROUTINE print_avg(x)
REAL :: x(:) ! an assumed-shape array
! Define the statement function get_avg.