|Fortran Language Reference Manual, Volume 1 - S-3692-51|
|Prev Section||Chapter 2. Fortran Concepts and Terms||Next Section|
A Fortran program must contain one main program and can contain any number of the other types of program units. Program units define data environments and the steps necessary to perform calculations. Each program unit has an END statement to terminate the program unit. Each program unit also has an identifying initial statement, but the identifying statement for a main program is optional.
An external subprogram (a function or a subroutine) performs a task or calculation on entities available to the external subprogram. These entities can be the arguments to the subprogram that are provided in the reference, entities defined in the subprogram, or entities made accessible by other means, such as common blocks. A CALL statement invokes a subroutine. A function is invoked when its value is needed in an expression. The computational process that is specified by a function or subroutine subprogram is called a procedure. It may be invoked from other program units of the Fortran program. Neither module nor block data program units are executable, so they are not considered to be procedures. A main program also contains executable constructs, but it is not classified as a procedure.
A block data program unit contains only data definitions. It specifies initial values for a restricted set of data objects.
A module contains public and protected definitions that can be made accessible to other program units and subprograms. A module also contains private definitions that are accessible only within the module. These definitions include data definitions, type definitions, definitions of subprograms known as module subprograms, and specifications of procedure interfaces. Module subprograms can be either subroutine or function subprograms. The procedures they define are called module procedures. Module subprograms can be invoked by other module subprograms in the module or by other program units that access the module.
A main program, external subprograms, and module subprograms may contain internal subprograms, which can be either subroutines or functions. The procedures they define are called internal procedures. Internal subprograms must not themselves contain internal subprograms, however. The main program, external subprogram, or module subprogram that contains an internal subprogram is referred to as the internal subprogram's host. Internal subprograms may be invoked by their host or by other internal subprograms in the same host.
A statement function also defines a procedure. Executable program units, module subprograms, and internal subprograms can all contain statement functions.
Figure 2-1 illustrates the organization of a sample Fortran program. Large arrows represent use association with the USE statement at the arrow tip. Small arrows represent subprogram references with the call at the arrow base.
Program units and subprograms are described more fully in the Fortran Language Reference Manual, Volume 2.
The most important benefit of packaging is the capability to hide information. Entities can be kept inaccessible except where they are actually needed. This provides some protection against inadvertent misuse or corruption, thereby improving program reliability. Packaging can make the logical structure of a program more apparent by hiding complex details at lower levels. Programs are therefore easier to comprehend and less costly to maintain. The Fortran features that provide these benefits are internal procedures and modules.
As previously noted, internal procedures can appear in a main program, external subroutines, external functions, and module subprograms. They are known only within their host. The name of an internal procedure must not be passed as an argument, and an internal procedure must not itself be the host of another internal procedure. However, a statement function can appear within an internal procedure. Thus, in some ways, internal procedures are like external procedures and in other ways they are like statement functions.
Modules provide the most comprehensive opportunities to apply packaging concepts, as illustrated in Figure 2-1. In addition to several levels of organization and hiding, the entities specified in a module (types, data objects, procedures, interfaces, and so on) can be kept private to the module or made available to other scoping units by use association.