HP Fortran Programmer's Guide (B3908-90031; September 2011)

Controlling data storage
Modules vs. common blocks
Chapter 3 117
Modules vs. common blocks
The common block has been a mainstay of Fortran programs throughout the evolution of the language, and
it continues to be a part of Fortran. The common block provides a convenient means to share data among
program units, especially when the program units sharing data do not otherwise communicate with each
other. The common block can also be used to share data between simultaneously executing Fortran
programs (see “Sharing data among programs” on page 113) and between Fortran program units and C
functions linked together in the same program (see “Sharing data” on page 200).
One of the problems with the common block, however, is that the programmer must replicate the COMMON
declaration in each of the sharing program units. If any of the common variables are out of order or have a
different type or size, the program units may not access the same data. The compiler gives no indication of
this discrepancy because it assumes that the programmer is giving one program unit a different view of the
shared storage—even when the discrepancy is owing to oversight.
To deal with this problem, many implementations of FORTRAN 77 have provided the INCLUDE extension.
This extension enables the user to centralize common block definitions in one file. At compile-time, the
compiler reads the file into program units that have the INCLUDE line. While this approach eliminates the
problem of discrepant common blocks, it introduces another problem: the INCLUDE facility is nonstandard
FORTRAN 77, and its use is nonportable.
To deal with the portability issue, Standard Fortran defines the INCLUDE line. Unfortunately, the definition
in the Standard leaves many of the details up to the implementation, so that use of the INCLUDE line in
Fortran programs still runs the risk of nonportability.
Another problem with the common block—especially when used with equivalencing—is that it can inhibit
optimization. Common block variables are generally ineligible for register allocation, and aliasing variables
in common can prevent the optimization of the program units that use the aliased variables.
The module program unit is the Fortran answer to the common block. The programmer declares shareable
variables in a module. Any program unit that wants to access them references the name of the module in a
USE statement. The concept of the module eliminates the need to re-declare the common variables, without
requiring the INCLUDE line.
In addition, the module provides the following controls on access to module data:
The PUBLIC and PRIVATE statements declare which module variables are accessible outside the
module and which are not.
The USE statement has an ONLY clause that specifies which module variables are accessible to a
particular program unit.
The USE statement also has a renaming feature to resolve name clashes between local variables and
module variables.