Multiple Entries into Subprograms [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP FORTRAN 77/iX Programmer's Guide
Multiple Entries into Subprograms
A subroutine call or function reference usually invokes the subprogram at
the entry point defined by the SUBROUTINE or FUNCTION statement. The
first statement executed is the first executable statement in the
subprogram. However, you can use the ENTRY statement to define other
entry points within the function or subroutine.
The entry point can be anywhere within a function or subroutine after the
FUNCTION or SUBROUTINE statement, but not within an IF block or DO loop.
The ENTRY statement can only be used in a subroutine or function
subprogram, not in a main program or block data subprogram. A subprogram
can have any number of ENTRY statements.
The ENTRY statement, a nonexecutable statement, looks like this:
ENTRY name(argument list)
where name is the entry point name, and the optional argument list is
made up of variable names, array names, dummy procedure names, or an
asterisk. The asterisk, indicating an alternate return, is permitted
only in a subroutine.
When an entry name is used to enter a subprogram, execution begins with
the first executable statement that follows the ENTRY statement. The
flow of control is illustrated in the following diagram.
PROGRAM main
|<---- CALL entry1(val)
| CALL entry2(val) ------>|
| |
| END |
| |
| SUBROUTINE sub |
| |
-----> ENTRY entry1(a) |
a = a + 5.0 |
RETURN ! Return to main |
|
ENTRY entry2(a) <--------
a = a + 10.0
END ! Return to main
A subroutine with entry points search and punctuation is shown below:
SUBROUTINE linka(d, n, f)
INTEGER d, n, f, table, document
C In subroutine linka, via primary entry point
DO 10 i = 1, f, n
.
.
.
10 CONTINUE
RETURN
ENTRY search(table, f)
C In subroutine linka, via entry point search
DO 20 i = 1, f
.
.
.
20 CONTINUE
RETURN
ENTRY punctuation(document)
C In subroutine linka, via entry point punctuation
DO 30 i = 1, 5
.
.
.
30 CONTINUE
END
In this subroutine, the names search and punctuation define alternate
entry points into subroutine linka.
The first statement executed in the subroutine is determined by the entry
point, as follows:
-------------------------------------------------------------
| | |
| Call to the Entry Point | First Statement Executed |
| | |
-------------------------------------------------------------
| | |
| CALL linka(var1, var2, var3) | DO 10 I = 1, f, n |
| | |
-------------------------------------------------------------
| | |
| CALL search(var1, var2) | DO 20 I = 1, f |
| | |
-------------------------------------------------------------
| | |
| CALL punctuation(var1) | DO 30 I = 1, 5 |
| | |
-------------------------------------------------------------
The order, type, and names of the dummy arguments in an ENTRY statement
can differ from the dummy arguments in the FUNCTION, SUBROUTINE, and
other ENTRY statements in the same subprogram. However, each reference
to a function or subroutine must use an actual argument list that agrees
in order, number, and type with the dummy argument list in the
corresponding FUNCTION, SUBROUTINE, or ENTRY statement. Type agreement
is not required for actual arguments that have no type, such as a
subroutine name or an alternate return specifier as an actual argument.
The following example shows a function with entry points of different
data types:
REAL FUNCTION f(x) ! Real function f
INTEGER k, i
.
.
.
ENTRY k(i) ! Integer function k
.
.
.
END
The declarations of the dummy arguments can precede their use in an ENTRY
statement. For example, in the following function:
FUNCTION x(array1, q)
INTEGER q
INTEGER array1(q)
.
.
.
ENTRY y (r, q)
.
.
.
array1(q) = 100
.
.
.
END
the variable q is declared before the ENTRY statement and the last
element of the array is set to 100.
In a function subprogram, a variable name that is the same as an entry
name cannot appear in any statement that precedes the appearance of the
entry name in an ENTRY statement, except in a type statement.
Within a subprogram, an entry name cannot appear both as an entry name in
an ENTRY statement and as a dummy argument in a FUNCTION, SUBROUTINE, or
ENTRY statement. An entry name cannot appear in an EXTERNAL statement.
MPE/iX 5.0 Documentation