Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 7 Program units and procedures

External procedures

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

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:

  • Procedure definition

  • Procedure reference

  • Returning from a procedure call

  • Alternate entry points

For detailed information about any of the statements associated with procedures (for example, SUBROUTINE and FUNCTION), refer to Chapter 10 “HP Fortran 90 statements”.

Procedure definition

The definition of an external procedure takes the form:

external-procedure-statement
[specification-part]
[execution-part]
[internal-procedure-part]
end-external-procedure-statement
external-procedure-statement

takes one of the following forms, depending on whether the procedure is a subroutine or function

  • [RECURSIVE] SUBROUTINE name &    [([dummy-arg-list])]
  • [RECURSIVE][type-spec] FUNCTION name &    ([dummy-arg-list]) [RESULT (result-name)]

where name is the name of the procedure; type-spec is the type of the function's result value; and dummy-arg-list is a comma-separated list of dummy arguments, as described in “Arguments”. The SUBROUTINE and FUNCTION statements are fully described in Chapter 10 “HP Fortran 90 statements”.

specification-part

is zero or more of the statements listed in Table 7-1 “Specification statements” as well as the AUTOMATIC statement.

execution-part

is zero or more of the statements listed in Table 7-2 “Executable statements” as well as the following statements:

  • ENTRY statement

  • RETURN statement

internal-procedure-part

takes the form:

CONTAINS     [internal-procedure]...internal-procedure

is the definition of an internal procedure; see “Internal procedures”.

end-external-procedure-statement

takes one of the following forms, depending on whether the procedure is a subroutine or function:

  • END [SUBROUTINE [subroutine-name]]
  • END [FUNCTION [function-name]]

Procedure reference

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.

Referencing a subroutine

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:

CALL subroutine-name [([actual-argument-list])]
actual-argument-list

is a comma-separated list of the actual arguments that take the form:

[keyword =] actual-argument

keyword

is the name of a dummy argument that appears in the SUBROUTINE statement. For more information about keyword, see “Keyword option”.

actual-argument

is one of:

  • Expression, including a variable name

  • Procedure name

  • Alternate return

For detailed information about arguments, see “Arguments”.

alternate-return

is one of:

  • *label

  • &label

label must be a branch target in the same scoping unit as the CALL statement. The ampersand prefix (&) is an HP extension and is permitted in fixed source form only. For information about alternate returns, see “Returning from a procedure reference”.

For information about referencing a subroutine that implements a defined assignment, see “Defined assignment”.

Referencing a function

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:

name ([actual-argument-list])

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”.

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)

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

PROGRAM main
! illustrates alternate return arguments

INTEGER :: por ! point of return

por = -1 ! interpreted by arithmetic IF
CALL subr(por, *10, *15) ! executes first
PRINT *, "Default returning point"
por = 0
CALL subr(por, *10, *15) ! executes second
GOTO 20 ! control should never reach here
10 PRINT *, "Line 10 in main"
por = 1
CALL subr(por, *10, *15) ! executes third
GOTO 20 ! control should never reach here
15 PRINT *, "Line 15 in main"
20 CONTINUE

END PROGRAM main

SUBROUTINE subr(where_to, *, *)
! Argument list includes placeholders for two alternate returns;
! the third argument, where_to, is used to select a return
! argument

INTEGER :: where_to

! use arithmetic IF to select a return
IF (where_to) 25, 30, 35 ! labels to transfer control
PRINT *, "Should never print"
25 PRINT *, "Line 25 in subr"
RETURN ! default returning point
30 PRINT *, "Line 30 in subr"
RETURN 1 ! select the first return argument
35 PRINT *, "Line 35 in subr"
RETURN 2 ! select the second return argument

END SUBROUTINE subr

Here are the command lines to compile and execute the program, along with the output from a sample run:

$ f90 alt_return.f90
$ a.out
Line 25 in subr
Default returning point
Line 30 in subr
Line 10 in main
Line 35 in subr
Line 15 in main

Alternate entry points

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:

ENTRY entry-name ([dummy-arg-list])[RESULT (result-name)]

Refer to “ENTRY” for a full description of the ENTRY statement.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.