FORTRAN 77 Language Reference

FUNCTION (External)

The FUNCTION statement identifies a program unit as a function subprogram.

[type] FUNCTION fun ([ar[, ar]])  

Parameter 

Description 

type

BYTE @

CHARACTER

CHARACTER*n (where n must be greater than zero)

CHARACTER*(*)

COMPLEX

COMPLEX*8 @

COMPLEX*16 @

COMPLEX*32 @

DOUBLE COMPLEX @

DOUBLE PRECISION

INTEGER

INTEGER*2 @

INTEGER*4 @

INTEGER*8 @

LOGICAL

LOGICAL*1 @

LOGICAL*2 @

LOGICAL*4 @

LOGICAL*8 @

REAL

REAL*4 @

REAL*8 @

REAL*16 @

fun

Symbolic name assigned to function 

ar

Formal argument name  

(COMPLEX*32 and REAL*16 are SPARC only.)

An alternate nonstandard syntax for length specifier is as follows: @

[ type ] FUNCTION name [* m]([ ar [,ar] ])

Parameter 

Description 

m

Unsigned, nonzero integer constant specifying length of the data type. 

ar

Formal argument name  

Description

Note the type, value, and formal arguments for a FUNCTION statement.

Type of Function

The function statement involves type, name, and formal arguments.

If type is not present in the FUNCTION statement, then the type of the function is determined by default and by any subsequent IMPLICIT or type statement. If type is present, then the function name cannot appear in other type statements.


Note -

Compiling with any of the options -dbl, -r8, -i2, or -xtypemap can alter the default data size assumed in the call to or definition of functions unless the data type size is explicitly declared. See Chapter 2 and the Fortran User Guide for details on these options.


Value of Function

The symbolic name of the function must appear as a variable name in the subprogram. The value of this variable, at the time of execution of the RETURN or END statement in the function subprogram, is the value of the function.

Formal Arguments

The list of arguments defines the number of formal arguments. The type of these formal arguments is defined by some combination of default, type statements, IMPLICIT statements, and DIMENSION statements.

The number of formal arguments must be the same as the number of actual arguments at the invocation of this function subprogram.

A function can assign values to formal arguments. These values are returned to the calling program when the RETURN or END statements are executed in the function subprogram.

Restrictions

Alternate return specifiers are not allowed in FUNCTION statements.

f77 provides recursive calls. A function or subroutine is called recursively if it calls itself directly. If it calls another function or subroutine, which in turn calls this function or subroutine before returning, then it is also called recursively.

Examples

Example 1: Character function:


       CHARACTER*5 FUNCTION BOOL(ARG) 
       BOOL = 'TRUE' 
       IF (ARG .LE. 0) BOOL = 'FALSE' 
       RETURN 
       END 

In the above example, BOOL is defined as a function of type CHARACTER with a length of 5 characters. This function when called returns the string, TRUE or FALSE, depending on the value of the variable, ARG.

Example 2: Real function:


       FUNCTION SQR (A) 
       SQR = A*A 
       RETURN 
       END 

In the above example, the function SQR is defined as function of type REAL by default, and returns the square of the number passed to it.

Example 3: Size of function, alternate syntax: @


       INTEGER FUNCTION FCN*2 ( A, B, C ) 

The above nonstandard form is treated as:


       INTEGER*2 FUNCTION FCN ( A, B, C )