Begins a function subprogram. Identifies the data type of the function and names the dummy arguments. Format: [prefx] FUNCTION nam [([p[,p]...])] [RESULT (r-nam)] prefx Is either: typ [kywd] kywd [typ] typ Is a data type. If you do not specify a data type, the data type of the function is implied from its name. If the data type is CHARACTER, you can specify CHARACTER*(*) to indicate an assumed-length function type -- the function type assumes the length of its definition in the program unit invoking it. Assumed- length character functions are obsolescent in Fortran 95. VSI Fortran flags obsolescent features, but fully supports them. kywd Is one of the following: RECURSIVE Permits direct recursion to occur. If a function is directly recursive and array valued, RESULT must also be specified. PURE Restricts the procedure from having side effects. ELEMENTAL Specifies PURE with certain constraints: o A dummy argument: - Must be scalar and cannot have the POINTER attribute - Cannot appear in a specification expression, except as an argument to the BIT_SIZE, KIND, or LEN intrinsic functions or the numeric inquiry intrinsic functions - Must not be * - Must not be a dummy procedure o The function result must be scalar and cannot have the POINTER attribute. An explicit interface must be visible to the caller of an ELEMENTAL procedure. If ELEMENTAL is specified, RECURSIVE must not be specified. nam Is a symbolic name for the function. The name must be unique among all global names in the program. The name is used as a variable within the function. The value of the variable is returned to the caller of the function as the value of the function. The name can be followed by * and the length of the data type. It must be one of the valid length specifiers for "typ". This length overrides the length specified or implied by the type. This length specification is not permitted if the length has already been specified following CHARACTER. p Is an unsubscripted variable name specifying a dummy argument. The arguments must agree in order, number, and type with the actual arguments of the statement invoking the function. A dummy argument must not be defined as an array with more elements than the actual argument holds. r-nam Is the name of the function result. This name must not be the same as the function name. The array declarator for a dummy argument can itself contain integer values that are dummy arguments or are references to a common block, providing for adjustable size arrays in functions. The upper bound of the array declarator for a dummy argument can be specified as an asterisk, in which case the upper bound of the dummy argument assumes the size of the upper bound of the actual argument. The size in a character string declarator for a dummy argument can be specified as an asterisk in parentheses (*) -- in which case the size of the actual argument is passed to the dummy argument. The values of the actual arguments in the invoking program unit become the values of the dummy arguments in the function. If you modify a dummy argument, the corresponding actual argument in the invoking program unit is also modified; the actual argument must be a variable if it is to be modified. If the actual argument is a character constant, the dummy argument can be either character or numeric in type, unless the name of the subprogram being invoked is a dummy argument in the invoking program unit. If the actual argument is a Hollerith constant, the dummy argument must be numeric. The FUNCTION statement must be the first statement of a function subprogram, unless an OPTIONS statement is specified. A function subprogram cannot contain a SUBROUTINE statement, a BLOCK DATA statement, a PROGRAM statement, or another FUNCTION statement. ENTRY statements can be included to provide multiple entry points to the subprogram. NOTE In a function, the function name identifier refers to the return value, not the function itself, unless an argument list is present. Therefore, it is not possible to pass a function as an argument to another routine from inside the function. For example, consider the following: INTEGER FUNCTION RECURSIVE_FUNCTION . . . CALL OTHERSUB (RECURSIVE_FUNCTION) The reference to RECURSIVE_FUNCTION in the CALL statement passes the function return value, not the function itself.
1 – RESULT Keyword
Specifies a name for the result variable of a function. Its name must be different from the name of the function. If RESULT is not specified, the function name is the result variable. All references to the function are references to the function result variable. If RESULT is specified, the result name is the result variable. In this case, all references to the function name are recursive calls, and the function name must not appear in specification statements. The following is an example of a recursive function specifying a RESULT variable: RECURSIVE FUNCTION FACTORIAL(P) RESULT(L) INTEGER, INTENT(IN) :: P INTEGER L IF (P == 1) THEN L = 1 ELSE L = P * FACTORIAL(P - 1) END IF END FUNCTION
2 – Function Reference
Transfers control and passes arguments to a function. Format: nam (p[,p]...) nam Is the name of the function or the name of an entry point to the function. p Is a value to be passed to the function. The value can be a constant, the name of a variable, the name of an array element, the name of an array, an expression, a substring, field reference, or the name of a subprogram or entry point to a subprogram (must be defined as external). You cannot specify more than 255 arguments.