FORTRAN-SO

Functions And Subroutines

An actual argument may also be a dummy argument as long as the dummy is part of a dummy argument list within the subprogram containing the external function reference.

5.2.1.2 FUNCTION Subprogram Limitations

A FUNCTION statement may be used only as the first statement of a FUNCTION subprogram. The subprogram itself can consist of any other statement except a SUBROUTINE, BLOCK DATA, or PROGRAM statement.

The name of the FUNCTION subprogram is global and cannot be the same as any other name in the subprogram, except for its use as a variable in the body of the sub- program. Within the subprogram, the only nonexecutable statement in which the name may appear is a type statement, and even this is not permitted if the type is specified in the FUNCTION statement. A FUNCTION subprogram name may not be type character.

The symbolic name of a dummy argument in a function subprogram is local to the program unit and cannot be used in an EQUIVALENCE, SAVE, INTRINSIC, DATA, or COMMON statement (except as a common block name).

A function specified by a subprogram can be referenced within any other external procedure or in the main program. A function subprogram must.not reference itself, however, either directly or indirectly (but see the description of the REENTRANT compiler control in section F.2.3).

When an external function reference is executed, the function must be part of the program. External functions created outside the program must be linked to the pro- gram before it is executed. External procedure linkage is described in the FOR- TRAN compiler operator's manual.

Example:

CTHE FOLLOWING EXAMPLE TOTALS THE C VALUES IN AN ARRAY OF LENGTH I

FUNCTION TOTAL(ARRAY, I) DIMENSION ARRAY(I) TOTAL = 0.0

DO 100 K = 1, I

TOTAL = TOTAL + ARRAY(K)

100CONTINUE RETURN

END

5.2.2Subroutines

A subroutine is used primarily to avoid redundant blocks of code. A subroutine can be called from anywhere in a program, with actual arguments substituted for the dummy arguments specified in the subroutine definition. The subroutine performs its operations, then returns control to the point of call.

The SUBROUTINE statement must be the fitst statement of a SUBROUTINE sub- program. The subprogram (being a program unit) must be terminated by an END statement and can contain any statement except anotqer SUBROUTINE statement, or a FUNCTION, BLOCK DATA, or PROGRAM statement. At some point in the subprogram, the RETURN statement can be specified to tell the subroutine when to return control to the point of call.

5-5

Page 53
Image 53
Intel fortran-80 Subroutines, Function Subprogram Limitations, Do 100 K = 1, Total = Total + Arrayk 100CONTINUE Return END