A program unit is a sequence of one or more lines, organized as statements, comments, and INCLUDE 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.
Names of main programs, external procedures, block data program units, and modules are global entities. Names of 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:
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 A MUST BE PASSED END SUBROUTINE SUB(A) INTEGER A,B,C ! A must be redeclared C=A+B END SUBROUTINE
Fortran 90 or Fortran 95 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:
The characteristics of a procedure consist of:
The characteristics of a dummy data object are its type, type parameters (if any), shape, intent, whether it is optional, whether it is allocatable, whether it is a pointer, and whether it is a target. 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:
Explicit in a scoping unit if an interface|
block exists or is accessible
Implicit in all other cases
Explicit in a scoping unit other than its own|
if an interface block exists or is accessible
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.
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.
A procedure must have an explicit interface if:
A procedure has an implicit interface if its interface is not fully known; that is, it has no explicit interface.