A program unit is a sequence of one or more lines, organized as statements, comments, and directives. Specifically, a program unit can be:
An executable program is a collection of program units consisting of one main program and any number of external subprograms, modules, and block data program units.
A subprogram can be invoked by a main program or by another subprogram to perform a particular activity. When a procedure is invoked, the referenced subprogram is executed.
An external or module subprogram can contain multiple ENTRY statements. The subprogram defines a procedure for the SUBROUTINE or FUNCTION statement, as well as one procedure for each ENTRY statement.
An external procedure is defined either by an external subprogram or by a program unit in a programming language other than Fortran.
Main programs, external procedures, block data program units, common blocks, entities with binding labels, and modules are global entities. Internal and module procedures are local entities.
External subprograms, module subprograms, and main programs can have internal subprograms, whether the internal subprograms are functions or subroutines, as long as the internal subprograms follow the CONTAINS statement.
An internal procedure is defined by an internal subprogram. Internal subprograms cannot appear in other internal subprograms. A module procedure is defined by a module subprogram or an entry in a module subprogram. Internal procedures and module procedures are the same as external procedures except that:
Migration Tip:
Turn your external procedures into internal subprograms or put them into modules. The explicit interface provides type checking.
FORTRAN 77 source
PROGRAM MAIN
INTEGER A
A=58
CALL SUB(A) ! C must be passed
END
SUBROUTINE SUB(A)
INTEGER A,B,C ! A must be redeclared
C=A+B
END
Fortran 90/95/2003 source:
PROGRAM MAIN INTEGER :: A=58 CALL SUB CONTAINS SUBROUTINE SUB INTEGER B,C C=A+B ! A is accessible by host association END SUBROUTINE END
The interface of a procedure determines the form of the procedure reference. The interface consists of:
To ascertain the characteristics of a procedure:
The characteristics of a dummy
data object are its type, type parameters (if any), shape, intent, whether
it is optional, allocatable, a pointer, a target, or has the
VALUE
attribute.
Any dependence on other objects for type parameter or array bound determination
is a characteristic. If a shape, size, or character length is assumed, it
is a characteristic.
The characteristics of a dummy procedure are the explicitness of its interface, its procedure characteristics (if the interface is explicit), and whether it is optional.
If a procedure is accessible in a scoping unit, it has an interface that is either explicit or implicit in that scoping unit. The rules are:
Entity | Interface |
---|---|
Dummy procedure | Explicit in a scoping unit if an interface block exists or is accessible, or if an explicit interface is specified by a PROCEDURE declaration statement. Implicit in all other cases. |
External subprogram | Explicit in a scoping unit other than its own if an interface block exists or is accessible, or if an explicit interface is specified by a PROCEDURE declaration statement. Implicit in all other cases. |
Recursive procedure with a result clause | Explicit in the subprogram's own scoping unit. |
Module procedure | Always explicit. |
Internal procedure | Always explicit. |
Generic procedure | Always explicit. |
Intrinsic procedure | Always explicit. |
Statement function | Always implicit. |
Internal subprograms cannot appear in an interface block or in a PROCEDURE declaration statement.
A procedure must not have more than one accessible interface in a scoping unit.
The interface of a statement function cannot be specified in an interface block or in a PROCEDURE declaration statement.
A procedure must have an explicit interface in any of the following cases:
A procedure has an implicit interface if its interface is not fully known; that is, it has no explicit interface.