Internal procedures

FTN95 provides internal procedures to allow even a few lines of coding to be used as a "subroutine" without the run-time overhead that a CALL statement produces. These are mainly provided for compatibility with FTN77, since Fortran 95 allows internal sub-programs via the CONTAINS statement, although internal procedures are still useful in run-time sensitive code.

As its name suggests, an internal procedure is local to the program unit in which it appears. FTN95 provides three statements to deal with internal procedures, and extends the use of the EXIT statement:

INTERNAL PROCEDURE <list of int-proc-names>

INVOKE <int-proc-name>

PROCEDURE <int-proc-name>

EXIT <int-proc-name>

 

The INTERNAL PROCEDURE statement

INTERNAL PROCEDURE is a specification statement and must appear before any executable statement in a program unit. The general form is:

INTERNAL PROCEDURE <list of int-proc-names>

where <list of int-proc-names> is a list of internal procedure names separated by commas. Every internal procedure name used in a program unit must first appear in an INTERNAL PROCEDURE statement.

 

The PROCEDURE statement

The PROCEDURE statement is used to define the start of an internal procedure. It has the form:

PROCEDURE <int-proc-name>

An internal procedure has no argument list; any local or external name available to the program unit in which the internal procedure appears is available for use within the procedure.

Note:
When defining an internal procedure, it is up to the programmer to ensure that there is no possibility of control "flowing into" the procedure. It is suggested that internal procedure definitions are grouped together following a RETURN or GOTO statement at the end of a program unit, for example:

. . .
RETURN
PROCEDURE P1
. . .
PROCEDURE P2
. . .
END

 

The EXIT statement

The EXIT statement is used to exit from an internal procedure. It may appear anywhere in an internal procedure definition and takes the form:

EXIT <int-proc-name>

An EXIT statement can appear wherever an executable statement is allowed (for example, at the end of an IF statement).

Care should be taken to ensure that the name of the internal procedure is not the same as any construct-label in the routine otherwise the EXIT statement could be ambiguous.

The only effect of an EXIT statement is to transfer control to the statement following the INVOKE statement used to invoke the internal procedure.

More than one EXIT statement can appear in an internal procedure definition. The EXIT statement need not necessarily be the last statement in a definition so that remarks made at the end of the previous section again apply here.

A program may also exit an internal procedure by executing a RETURN statement, which also leaves the parent routine.

 

The INVOKE statement

The INVOKE statement is used to "call" an internal procedure. It has the general form:

INVOKE <int-proc-name>

and can appear anywhere that an executable statement is allowed. The only effect of an INVOKE statement is to transfer control to the specified internal procedure.

Example of the use of an internal procedure

INTERNAL PROCEDURE ERROR
. . .
N = 7
INVOKE ERROR
. . .
N = 80
INVOKE ERROR
. . .
PROCEDURE ERROR
IF (N.LT.50) THEN
CALL ERROR1(N)
ELSE
 CALL ERROR2(N)
ENDIF
EXIT ERROR
. . .
END

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited