| United States-English |
|
|
|
![]() |
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 7 Program units and proceduresExternal procedures |
|
External procedures are implemented as either functions or subroutines. The major difference between the two is that a function subprogram returns a value and can therefore appear as an operand in an expression. The following sections describe both types of external procedures, including the following topics:
For detailed information about any of the statements associated with procedures (for example, SUBROUTINE and FUNCTION), refer to Chapter 10 “HP Fortran 90 statements”. The definition of an external procedure takes the form:
A procedure reference—also known as a procedure call—occurs when a procedure name is specified in an executable statement, which causes the named procedure to execute. The following sections describe references to subroutines and functions, and recursive references—when a procedure directly or indirectly calls itself. A reference to an external subroutine occurs in a CALL statement, which specifies either the subroutine name or one of its entry point names. The syntax of the CALL statement is:
For information about referencing a subroutine that implements a defined assignment, see “Defined assignment”. An external function subprogram is referenced either by its name or by one of its entry point names. The syntax of a function reference is:
where name is the function name or the name of one of its entry points (see “Alternate entry points”). actual-argument-list has the same as it does in a subroutine reference (see “Procedure reference”), except that it may not include an alternate return. For information about referencing a function that implements a defined operator, see “Defined operators”. 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:
Both internal and external procedures can be recursive. 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:
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:
then the actual arguments must include two labels for alternate returns, as in the following call:
As a compatibility extension, HP Fortran 90 allows the ampersand (&) as a prefix character instead of the asterisk, but only in fixed source form. Alternate returns cannot be optional, and the associated actual argument cannot have keywords. For detailed information about the syntax of the alternate return argument, refer to the descriptions of the CALL and RETURN statements in Chapter 10 “HP Fortran 90 statements”. The following example, alt_return.f90, illustrates the alternate return mechanism. The referenced subroutine, subr, selects one of two alternate return arguments based on the value of the first argument, where_to. Example 7-1 alt_return.f90
Here are the command lines to compile and execute the program, along with the output from a sample run:
When a procedure is referenced, execution normally begins with the first executable statement in the procedure. Using the ENTRY statement, however, the programmer can define alternate entry points into the procedure and associate a name with each entry point. Each ENTRY statement within a procedure defines a procedure entry, which can be referenced by name as a separate, additional procedure. The syntax for the ENTRY statement is:
Refer to “ENTRY” for a full description of the ENTRY statement. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||