 |
» |
|
|
 |
Introduces
a function subprogram. Syntax[RECURSIVE] [type-spec] FUNCTION |
function-name ([dummy-arg-name-list]) |
 |
 |
 |
- RECURSIVE
is a keyword that must
be specified in the FUNCTION statement if the function is either directly or
indirectly recursive. The RECURSIVE clause can appear at most once, either before
or after type-spec. It is not an error to specify RECURSIVE for a nonrecursive function. A recursive function that calls itself directly must also
have the RESULT clause specified (see below). - type-spec
is a valid type specification (INTEGER, REAL, LOGICAL, CHARACTER, TYPE (name), etc.). The type and type parameters of the function
result can be specified by type-spec or by declaring the result variable within the function
subprogram, but not by both. The implicit typing rules apply if
the function is not typed explicitly. If the function result is array-valued or a pointer, the appropriate
attributes for the result variable (which is function-name, or result-name if specified) must be specified within the function
subprogram. - function-name
is the name of the function subprogram being defined. - dummy-arg-name-list
is a comma-separated
list of dummy argument names for the function. - result-name
is the result
variable. If the RESULT clause is not specified, function-name becomes the result variable. If result-name is given, it must differ from function-name, and function-name must not then be declared within the function subprogram. As noted above, a recursive function that calls itself directly
must have the RESULT clause specified. For other functions, the RESULT clause is optional.
DescriptionA FUNCTION statement introduces an external, module, or internal
function subprogram. ExamplesPROGRAM main ... CONTAINS ! f is an internal function FUNCTION f(x) f = 2*x + 3 END FUNCTION f ! recursive function, which must specify RESULT clause RECURSIVE INTEGER FUNCTION factorial (n) & RESULT (factorial_value) IMPLICIT INTEGER (a-z) IF (n <= 0) THEN factorial_value = 1 ELSE factorial_value = n * factorial (n-1) END IF END FUNCTION factorial END PROGRAM main |
Related statementsCONTAINS, END, INTENT, INTERFACE, OPTIONAL, and the type declaration statements Related conceptsFor related information, see the following:
|