FORTRAN 77 Language Reference

Statement Function

A statement function statement is a function-like declaration, made in a single statement.

fun ([d[, d]]) = e

Parameter 

Description 

fun

Name of statement function being defined 

d

Statement function dummy argument  

e

Expression. e can be any of the types arithmetic, logical, or character.

Description

If a statement function is referenced, the defined calculations are inserted.

Example: The following statement is a statement function:


       ROOT( A, B, C ) = (-B + SQRT(B**2-4.0*A*C))/(2.0*A) 

The statement function argument list indicates the order, number, and type of arguments for the statement function.

A statement function is referenced by using its name, along with its arguments, as an operand in an expression.

Execution proceeds as follows:

  1. If they are expressions, actual arguments are evaluated.

  2. Actual arguments are associated with corresponding dummy arguments.

  3. The expression e, the body of a statement function, is evaluated.

  4. If the type of the above result is different from the type of the function name, then the result is converted.

  5. Return the value.

The resulting value is thus available to the expression that referenced the function.

Restrictions

Note these restrictions:

Examples

Example 1: Arithmetic statement function:


       PARAMETER ( PI=3.14159 ) 
       REAL RADIUS, VOLUME 
       SPHERE ( R ) = 4.0 * PI * (R**3) / 3.0 
       READ *, RADIUS 
       VOLUME = SPHERE( RADIUS ) 
       ... 

Example 2: Logical statement function:


       LOGICAL OKFILE 
       INTEGER STATUS 
       OKFILE ( I ) = I .LT. 1 
       READ( *, *, IOSTAT=STATUS ) X, Y 
       IF ( OK FILE(STATUS) ) CALL CALC ( X, Y, A ) 
       ... 

Example 3: Character statement function:


       CHARACTER FIRST*1, STR*16, S*1
       FIRST(S) = S(1:1) 
       READ( *, * ) STR 
       IF ( FIRST(STR) .LT. " " ) CALL CONTROL ( S, A ) 
       ...