8.7.4 Using COMMON and MAP Statements in
Subprograms
The COMMON and MAP statements create a block of stor-
age called a PSECT. This common or map storage block is
accessible to any subprogram. A BASIC main program and a
subprogram can share such an area by referencing the same
common or map name.
The following example contains common blocks that define:
If a subprogram defines a common or map area with the
same name as a common or map area in the main program,
it overlays the common or map defined in the main program.
Multiple COMMON statements with the same name behave
differently depending on whether these statements are in
the same program module. If they are in the same program
module, then the storage for each common area is concate-
nated. However, if they are in different program units, then
the common areas overlay the same storage. The follow-
ing COMMON statements are in the same program module;
therefore, they are concatenated in a single PSECT. The
PSECT contains two 32-byte strings.
COMMON (XYZ) STRING A = 32
COMMON (XYZ) STRING B = 32
In contrast, the following COMMON statements are in dif-
ferent program modules, and thus overlay the same storage.
Therefore, the PSECT contains one 32-byte string, called A
in the main program and B in the subprogram.
!In the main program
COMMON (XYZ) STRING A = 32
.
.
.
!In the subprogram
COMMON (XYZ) STRING B = 32
Although you can redefine the storage in a common section
when you access it from a subprogram, you should gener-
ally not do so. Common areas should contain exactly the
same variables in all program modules. To make sure of this,
you should use the %INCLUDE directive, as shown in the
following example:
COMMON (SHARE) WORD emp_num, &
DECIMAL (8,0) salary, &
STRING wage_class = 2
.
.
.
!In the main program
%INCLUDE "COMMON.BAS"
.
.
.
!In the subprogram
%INCLUDE "COMMON.BAS"
If you use the %INCLUDE directive, you can lessen the
chance of a typographical error appearing in your program.
For more information about using the %INCLUDE directive,
see Chapter 17.
If you must redefine the variables in a PSECT, you should use
the MAP statement or a record with variants for each over-
lay. When you use the MAP statement, use the %INCLUDE
directive to create identical maps before redefining them,
as shown in the following example. The map defined in
MAP.BAS is included in both program modules as a 40-byte
string. This map is redefined in the subprogram, allowing the
subprogram to access parts of this string.
MAP (REDEF) STRING full_name = 40
.
.
.
!In the main program
%INCLUDE "MAP.BAS"
.
.
.
!In the subprogram
%INCLUDE "MAP.BAS"
MAP (REDEF) STRING first_name=15, MI=1, last_name=24