Programmer Guide > Statement Types > Common Block Definition Statement
  

Common Block Definition Statement
COMMON block_name, var1, var2,..., varn
The Common Block Definition statement creates a Common Block with the designated name (up to 31 characters long) and places the variables whose names follow into that block. Variables defined in a Common Block may be referenced by any program unit that declares that Common Block.
A Common Block Definition statement is useful when there are variables which need to be accessed by several procedures. Any program unit referencing a Common Block may access variables in the block as though they were local variables. Variables in a Common statement have a global scope within procedures defining the same Common Block. Unlike local variables, variables in Common Blocks are not destroyed when a procedure is exited.
The number of variables appearing in the Common Block Definition statement determines the size of the Common Block. The first program unit (main program, function, or procedure) defining the Common Block sets the size of the Common Block, which is fixed. Other program units may reference the Common Block with the same or fewer number of variables.
Common Blocks share the same space for all procedures. Common Block variables are matched variable to variable, unlike FORTRAN, where storage locations are matched. The third variable in a given Common Block will always be the same as the third variable in all declarations of the Common Block regardless of the size, type or structure of the preceding variables.
The two procedures in the following example show how variables defined in Common Blocks are shared:
PRO ADD, A
COMMON SHARE1, X, Y, Z, Q, R
A = X + Y + Z + Q + R
PRINT, X, Y, Z, Q, R, A
RETURN
END
PRO SUB, T 
COMMON SHARE1, A, B, C, D 
T = A - B - C - D 
PRINT, A, B, C, D, T 
RETURN
END
The variables X, Y, Z, and Q in the procedure ADD are the same as the variables A, B, C, and D, respectively, in procedure SUB. The variable R in ADD is not used in SUB. If the procedure SUB were to be compiled before the procedure ADD, an error would occur when the COMMON definition in ADD was compiled. This is because SUB has already declared the size of the Common Block, SHARE1, which may not be extended.
Variables in Common Blocks may be of any type and may be used in the same manner as normal variables. Variables appearing as parameters may not be used in Common Blocks. There are no restrictions in regard to the number of Common Blocks used, although each Common Block uses dynamic memory.
 
note
It is useful when using Common Blocks to declare them once in a PV‑WAVE command file and then include them where they are needed using the '@' operator. This ensures that all Common Block definitions are identical regardless of compilation order:
PRO ADD, A
   @declare_commons
   ; procedure code...
END
For more information about the at ('@') operator, refer to Chapter 24: Special Characters in the PV‑WAVE Reference and Creating and Running a Command (Batch) File.

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.