A subroutine subprogram is invoked in a CALL statement or by a defined assignment statement, and does not return a particular value.
The SUBROUTINE statement is the initial statement of a subroutine subprogram. It takes the following form:
[prefix] SUBROUTINE name [([d-arg-list])]
Keyword | Meaning |
---|---|
RECURSIVE | Permits direct recursion to occur (see Section 8.4.1.1). |
PURE | Asserts that the procedure has no side effects (see Section 8.4.1.2). |
ELEMENTAL | Restricted form of pure procedure that acts on one array element at a time (see Section 8.4.1.3). |
EXTRINSIC(HPF)[1] | Causes the procedure to execute in the single-threaded model known as "global HPF" -- essentially the standard Fortran model, but with some extensions and a few restrictions to allow for better parallelization. This is the default if no extrinsic prefix is specified (either explicitly or implicitly in the declaration of a host module or subprogram). |
EXTRINSIC(HPF_LOCAL)[1] | Causes multiple independent copies of the same procedure to execute on different data on multiple processors. |
EXTRINSIC(HPF_SERIAL)[1] | Causes the procedure to execute as if it were a single process executing on a scalar computer system. |
[1] This keyword is allowed on all platforms, but is functional only in parallel programs on Tru64 UNIX systems. For more information on EXTRINSIC keywords, see the Compaq High Performance Fortran 90 HPF and PSE Manual. |
A subroutine is invoked by a CALL statement or defined assignment. When a subroutine is invoked, dummy arguments (if present) become associated with the corresponding actual arguments specified in the call.
Execution begins with the first executable construct or statement following the SUBROUTINE statement. Control returns to the calling program unit once the END statement (or a RETURN statement) is executed.
A subroutine subprogram cannot contain a FUNCTION statement, a BLOCK DATA statement, a PROGRAM statement, or another SUBROUTINE statement. ENTRY statements can be included to provide multiple entry points to the subprogram.
The following example shows a subroutine:
Main Program Subroutine CALL HELLO_WORLD SUBROUTINE HELLO_WORLD ... PRINT *, "Hello World" END END SUBROUTINE
The following example uses alternate return specifiers to determine where control transfers on completion of the subroutine:
Main Program Subroutine CALL CHECK(A,B,*10,*20,C) SUBROUTINE CHECK(X,Y,*,*,Q) TYPE *, 'VALUE LESS THAN ZERO' ... GO TO 30 50 IF (Z) 60,70,80 10 TYPE*, 'VALUE EQUALS ZERO' 60 RETURN GO TO 30 70 RETURN 1 20 TYPE*, 'VALUE MORE THAN ZERO' 80 RETURN 2 30 CONTINUE END ...
The SUBROUTINE statement argument list contains two dummy alternate return arguments corresponding to the actual arguments *10 and *20 in the CALL statement argument list.
The value of Z determines the return, as follows:
(An alternate return is an obsolescent feature in Fortran 90 and Fortran 95.)