02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Program units and procedures<br />

External procedures<br />

Recursive reference<br />

A procedure that directly or indirectly invokes itself is recursive. Such a procedure must<br />

have the word RECURSIVE added to the FUNCTION or SUBROUTINE statement.<br />

If a function calls itself directly, both RECURSIVE and a RESULT clause must be specified in the<br />

FUNCTION statement, making its interface explicit.<br />

The following is a recursive function:<br />

RECURSIVE FUNCTION factorial (n) RESULT(r)<br />

INTEGER :: n, r<br />

IF (n.ne.0) THEN<br />

r = n*factorial(n-1)<br />

ELSE<br />

r = 1<br />

ENDIF<br />

END FUNCTION factorial<br />

Both internal and external procedures can be recursive.<br />

Returning from a procedure reference<br />

When the END statement of a subprogram is encountered, control returns to the calling<br />

program unit. The RETURN statement can be used to the same effect at any point within a<br />

procedure. The syntax of the RETURN statement is:<br />

RETURN [alt-return-arg]<br />

where alt-return-arg is a scalar integer expression that evaluates to the position of one of<br />

an alternate-return argument in the subroutine argument list. alt-return-arg is not<br />

permitted with RETURN statements appearing in functions.<br />

By default, when control returns from a subroutine call, the next statement to execute is the<br />

first executable statement following the CALL statement. However, by specifying alternate<br />

returns as actual arguments in the subroutine call, the programmer can return control to<br />

other statements. The alternate returns are labels prefixed with an asterisk (*). Each label is<br />

inserted in the list of actual arguments in the position that corresponds to a placeholder—a<br />

simple asterisk (*)—in the dummy argument list. For example, if the subroutine subr has the<br />

following list of dummy arguments:<br />

SUBROUTINE subr(x, y, z, *, *)<br />

then the actual arguments must include two labels for alternate returns, as in the following<br />

call:<br />

CALL subr(a, b, c, *10, *20)<br />

132<br />

Chapter 7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!