Common Blocks [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP FORTRAN 77/iX Programmer's Guide
Common Blocks
In addition to passing values through arguments, common blocks can
provide communication between program units and subprograms. Before a
FORTRAN program is executed, computer storage is allocated for each
program and subprogram. In addition, a common block of storage is
reserved for use by all program units. Program units define the data to
be reserved in common blocks with the COMMON statement.
There are two kinds of common blocks: blank common and labeled common.
Blank Common Blocks
The blank COMMON statement looks like this:
COMMON list
where list is variable names, array names, or array names with declared
dimensions, all separated by commas. The COMMON statement instructs the
compiler to establish an area of storage shared by all program units
using blank COMMON statements.
For example, if the statements:
REAL time, distance, car(2,3)
INTEGER count
COMMON car, count, time, distance
are in a program unit A, during execution, the common storage is
organized as follows:
---------------------------------
| | |
| Word | Item |
| | |
---------------------------------
| | |
| 1 | car(1,1) |
| | |
---------------------------------
| | |
| 2 | car(2,1) |
| | |
---------------------------------
| | |
| 3 | car(1,2) |
| | |
---------------------------------
| | |
| 4 | car(2,2) |
| | |
---------------------------------
| | |
| 5 | car(1,3) |
| | |
---------------------------------
| | |
| 6 | car(2,3) |
| | |
---------------------------------
| | |
| 7 | count |
| | |
---------------------------------
| | |
| 8 | time |
| | |
---------------------------------
| | |
| 9 | distance |
| | |
---------------------------------
If a program unit B contains the same set of statements, each reference
made to car, count, time, or distance references the same storage
accessed by program unit A.
Within another program unit, the same data can be known by a different
symbolic name. For example, if program unit C contains the statements:
REAL array1(2), array2(3)
INTEGER i, j
COMMON i, array1, j, array2
The common block would be accessed as follows:
----------------------------------------------------------
| | | |
| Word | Item Name From | Item Name From |
| | Program Unit C | Program Unit A |
| | | |
----------------------------------------------------------
| | | |
| 1 | i | car(1,1) |
| | | |
----------------------------------------------------------
| | | |
| 2 | array1(1) | car(2,1) |
| | | |
----------------------------------------------------------
| | | |
| 3 | array1(2) | car(1,2) |
| | | |
----------------------------------------------------------
| | | |
| 4 | j | car(2,2) |
| | | |
----------------------------------------------------------
| | | |
| 5 | array2(1) | car(1,3) |
| | | |
----------------------------------------------------------
| | | |
| 6 | array2(2) | car(2,3) |
| | | |
----------------------------------------------------------
| | | |
| 7 | array2(3) | count |
| | | |
----------------------------------------------------------
As you can see, inconsistent COMMON statements make a program hard to
follow. To avoid errors, modules containing a COMMON statement should
specify the same organization of the common area. This can be
accomplished by declaring the common area in an INCLUDE file, and
including the file in each subprogram that needs it.
As an extension to the ANSI standard, variables in blank common blocks
can be initialized using DATA statements.
An example of how common blocks can pass values to and from subprograms
is shown below. The variable q in the main program shares storage space
with x in the subroutine. When a value for q is determined by the READ
statement, x automatically shares this value. Similarly, r and y also
share storage space, as does the variable side in the main program and in
the subprogram. The subroutine uses the values input for q and r to
compute the length of the hypotenuse of a right triangle.
PROGRAM comex
COMMON q, r, side
READ *, q, r
CALL tri
PRINT *, side
END
SUBROUTINE tri
COMMON x, y, side
side = SQRT(x**2 + y**2)
END
Labeled Common Blocks
In some programs, you might want to subdivide the common area into
smaller blocks with each block having a unique name. To do this, the
labeled form of the COMMON statement is used as follows:
COMMON /name/list, ..., /name/list
where name is the common block name and list is the list of variable
names, arrays, and array declarators.
Before a program is executed, one block of storage is allocated for each
unique named common area that was specified from the program units.
Labeled common blocks allow each program unit to have its own named
common area.
For example, the statement:
COMMON /block1/a, b, c, /block2/x, y, z
defines these two labeled common blocks:
Common Block block1:
------------------
| |
| a |
| |
------------------
| |
| b |
| |
------------------
| |
| c |
| |
------------------
Common Block block2:
------------------
| |
| x |
| |
------------------
| |
| y |
| |
------------------
| |
| z |
| |
------------------
To see how labeled common blocks work, consider this partial program:
PROGRAM main
COMMON var1, var2, var3
COMMON /block1/ var4, var5, var6
.
.
.
END
SUBROUTINE sub(x)
COMMON /block1/ a, b, c
.
.
.
END
FUNCTION func(y)
COMMON f1, f2, f3
.
.
.
END
The items var4, var5, and var6 in the main program are in the common
block named block1. The same storage words are referred to by the names
a, b, and c in subroutine sub. The items var1, var2 and var3 in the main
program are in blank common. The same storage words are used by the
names f1, f2, and f3 in function func.
Unlike local variables in a subprogram, items in blank and named common
blocks remain defined after the execution of a RETURN or END statement in
a subprogram.
Arrays and variables in labeled or blank common can be initialized by
using DATA statements in a BLOCK DATA subprogram.
MPE/iX 5.0 Documentation