This division describes all data the program
reads in
processes
creates
shares with other programs
Using the DATA DIVISION is optional, needed only if the program does use files, variables, or receives parameters. In reality, in almost every program there is a DATA DIVISION/WORKING-STORAGE section.
EXAMPLE FOR SYNTAX OF DATA DIVISION:
The sections are optional but must follow the order shown in the syntax.
Record structure of external files. Needed when the program reads or writes files.
Contains:
FD/SD lines
01-level records
fields (05, 10, ...)
EXAMPLE FOR FILE SECTION:
All the variables used by the program. These variables:
created and initialized at start of the program
static within the program live cycle
exist until the end of program
typically counters, flags, buffers
similar to global variables in a Java class
modules can not access them directly
modules can access via the CALL instruction the variables but not the whole WS section
best practice: counters, state switches, logical flags, persistent values, buffer fields
EXAMPLE FOR WORKING-STORAGE SECTION:
Variables which are created at every call. Features:
initialization upon every entry into the PROCEDURE DIVISION
every CALL resets the values
are freed upon exit
similar to local variables in a Java method
best practice: recursive calls, temporal variables, fields which must get initial values every times
EXAMPLE FOR LOCAL-STORAGE SECTION:
Parameters received from an other program or method. The actual program is called as a module from an other program. Features:
the caller program used the CALL instruction with variables
in this program, in the PROCEDURE DIVISION line the USING clause defines the received variables
the received parameters can be pointers to the original variables, or copies of them
RETURNING values returns a result value to the caller
not created but referenced via pointers from the caller module, the reference is established upon every call
EXAMPLE FOR LINKAGE SECTION:
More info under COBOL PROGRAM MODULES.
Under COBOL data is stored in hierarchical data model. The levels are signed by level-numbers (01, 05, 10, ...). The relationships among all data are defined through a system of level indicators and level-numbers.
EXAMPLE FOR DATA STRUCTURE:
EXAMPLE FOR DATA DIVISION SECTIONS: