About global and local in COBOL for the C++ and JAVA programmer.
Note:
I only know the details up till and including COBOL-85. There now is a new COBOL standard, informally known as, COBOL-2002. COBOL-2002 is a hybrid language, like C++, it is fully object oriented but still supports the old, non object oriented, standard (minus a few obsoleted statements).
Instead of 'global' or 'local' it might be better to use the more general term 'scope'.
The scope of a variable in a program having no contained (a.k.a. nested) programs always is global.
(code) example:
Code:
IDENTIFICATION DIVISION.
PROGRAM-ID. aProgram.
DATA DIVISION.
WORKING-STORAGE SECTION.
...
05 someVariable PIC .(..).
...
PROCEDURE DIVISION.
...
statements
...
END PROGRAM aProgram.
The variable 'someVariable' is known, and can be addressed, everywhere within the procedure division.
The scope of a variable in a program having one or more contained (a.k.a. nested) programs is defined by its location and the usage of the global keyword (example following).
(code) example:
Code:
IDENTIFICATION DIVISION.
PROGRAM-ID. mainProgram.
DATA DIVISION.
WORKING-STORAGE SECTION.
...
01 declaredGlobal GLOBAL.
05 variable1 PIC .(..).
01 NOTdeclaredGlobal.
05 variable2 PIC .(..).
...
PROCEDURE DIVISION.
...
statements
...
IDENTIFICATION DIVISION.
PROGRAM-ID. firstContainedProgram.
PROCEDURE DIVISION.
...
statements
...
END PROGRAM firstContainedProgram.
IDENTIFICATION DIVISION.
PROGRAM-ID. secondContainedProgram.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 variable3 PIC .(..).
PROCEDURE DIVISION.
...
statements
...
END PROGRAM secondContainedProgram.
END PROGRAM mainProgram.
Now the main program contains 2 nested programs:
(firstContainedProgram and secondContainedProgram)
The scope (visibility) of variable2 is the main program only. Variable1 can not be used in either firstContainedProgram or secondContainedProgram.
variable1 is declared 'GLOBAL' and the scope is therefore global. It can be used in the main program, or the first- or second-containedProgram.
variable3 is defined in the secondContainedProgram. Variable3 can only be used within the secondContainedProgram. It is therefore 'local' to the secondContainedProgram.
Contained (=nested) programs can only be called within the same source in which it is defined.
Contained programs can be nested indefinitely (that is: can themselves have contained programs).
Contained (=nested) programs have a hierarchy. This hierarchy is defined by the way these programs are nested and by the use of the 'COMMON' identifier. This 'COMMON' identifier plays a role in defining the visibility of the CALL structure.
For details of the above see the COBOL reference manual.
Answer to the JAVA (or C++) programmer:
Yes...local and global variables do exists, as explained above, but there is no direct 1 to 1 translation to JAVA.
Regards, Wim Ahlers.