FORTRAN Reference Manual

Program Units
FORTRAN Reference Manual528615-001
4-10
Recursion
Recursion
As an extension to the standard, HP FORTRAN permits recursive calls in
subprograms. The following program, which returns the factorial of a number, uses the
recursive function FACTORIAL:
INTEGER factorial, j
10 CONTINUE
READ (*,*, PROMPT = ' Enter argument: ', END=20) j
WRITE (*,*) ' Factorial is ', factorial(j)
20 CONTINUE
END
INTEGER FUNCTION factorial (n)
INTEGER n
IF (n .GT. 1) THEN
factorial = n * factorial (n-1)
ELSE
factorial = 1
END IF
END
Using Multiple Entry Points in Functions and
Subroutines
The ENTRY statement provides additional entry points for subroutine and function
subprograms. The ENTRY statement takes the form:
ENTRY name ( argument-list)
The ENTRY name, just like the subroutine and function name, must be unique to the
executable program. Likewise, the ENTRY argument list must agree in number, type,
and length with the actual argument list of the function reference or CALL statement
that executes the subprogram beginning with the ENTRY statement. In a function
subprogram of type character, every entry point must also be type character.