HP Fortran Programmer's Reference (September 2007)

Program units and procedures
External procedures
Chapter 7164
Recursive reference
A procedure that directly or indirectly invokes itself is
recursive
. Such a procedure must
have the word RECURSIVE added to the FUNCTION or SUBROUTINE statement.
If a function calls itself directly, both RECURSIVE and a RESULT clause must be specified in the
FUNCTION statement, making its interface explicit.
The following is a recursive function:
RECURSIVE FUNCTION factorial (n) RESULT(r)
INTEGER :: n, r
IF (n.ne.0) THEN
r = n*factorial(n-1)
ELSE
r = 1
ENDIF
END FUNCTION factorial
Both internal and external procedures can be recursive.
Returning from a procedure reference
When the END statement of a subprogram is encountered, control returns to the calling
program unit. The RETURN statement can be used to the same effect at any point within a
procedure. The syntax of the RETURN statement is:
RETURN [
alt-return-arg
]
where
alt-return-arg
is a scalar integer expression that evaluates to the position of one of
an alternate-return argument in the subroutine argument list.
alt-return-arg
is not
permitted with RETURN statements appearing in functions.
By default, when control returns from a subroutine call, the next statement to execute is the
first executable statement following the CALL statement. However, by specifying alternate
returns as actual arguments in the subroutine call, the programmer can return control to
other statements. The alternate returns are labels prefixed with an asterisk (*). Each label is
inserted in the list of actual arguments in the position that corresponds to a placeholder—a
simple asterisk (*)—in the dummy argument list. For example, if the subroutine subr has the
following list of dummy arguments:
SUBROUTINE subr(x, y, z, *, *)
then the actual arguments must include two labels for alternate returns, as in the following
call:
CALL subr(a, b, c, *10, *20)