02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Statement functions<br />

Program units and procedures<br />

Statement functions<br />

If an evaluation of a function with a scalar value can be expressed in just one <strong>Fortran</strong><br />

assignment statement, such a definition can be included in the specification part of a main<br />

program unit or subprogram. This definition is known as a statement function.Itislocalto<br />

the scope in which it is defined. The syntax is:<br />

function-name (dummy-argument-list) = scalar-expression<br />

All dummy arguments must be scalars. All entities used in scalar-expression must have<br />

been declared earlier in the specification part. A statement function can reference another<br />

statement function that has already been declared. The name cannot be passed as a<br />

procedure-name argument. A statement function has an explicit interface.<br />

The following example, stmt_func.f90, is the same as the one listed in “Internal procedures”<br />

onpage135exceptthatitimplementsget_avg as a statement function rather than as an<br />

internal function. As noted in the comments to the program, the elements of the array x are<br />

passed to the statement function as separate arguments because dummy arguments of a<br />

statement function must be scalars.<br />

Example 7-3 stmt_func.f90<br />

PROGRAM main<br />

! declare and initialize an array to pass to an external<br />

! procedure<br />

REAL, DIMENSION(3) :: values = (/2.0, 5.0, 7.0/)<br />

! Because the dummy argument to print_avg is an assumed-shape<br />

! array (see the definition of print_avg below), the<br />

! procedure interface of print_avg must be made<br />

! explicit within the calling program unit.<br />

INTERFACE<br />

SUBROUTINE print_avg(x)<br />

REAL :: x(:)<br />

END SUBROUTINE print_avg<br />

END INTERFACE<br />

CALL print_avg(values)<br />

END PROGRAM main<br />

! print_avg is an external subprogram<br />

SUBROUTINE print_avg(x)<br />

REAL :: x(:) ! an assumed-shape array<br />

! Define the statement function get_avg.<br />

Chapter 7 137

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!