COBOL - Divisions
COBOL division is the topmost component of the COBOL program and the most important one. This generally contains one or more sections which later divided into paragraphs. It is a collection of sections, paragraphs, sentences, or a block of characters. The starting line of a division is the division name, and the end line of division is the beginning of the next division or the end of the program. Divisions are System-defined words and all divisions should be in Area A.
A COBOL program can be split into the following four divisions:
- Identification division
- Environment division
- Data division
- Procedure division
The identification division is the first and the compulsory division of every COBOL program. In this division, the programmer write the details about the program like author name, date of execution, date of writing the code, etc. This division is used to identify the program and also specifies the name and type of the source element and is where classes and interfaces are specified.
In this division, we have to define the PROGRAM-ID (a keyword) immediately after the Identification division which specifies the program name that can contain 1 to 30 characters.
When you compile and execute the above program, it will display the following output:
The environment division is used to specify the program features such as files and character sets that depend on the system running it. Here, we write the details about the computer environment in which the program is written and executed. It is also used to specify the input and output files to the program. The environment division consists of two sections: Configuration section and Input-Output section.
- Configuration section: The configuration section is used to give information about the system on which the program is written and executed. It contains two paragraphs: Source computer to compile the program, and object computer to execute the program.
- Input-Output section: The input-output section is used to give information about the files we use in the program. It also contains two paragraphs: File control to provide information of external data sets we have used in the program, and I-O control to provide information of files that we have used in the program.
It consists of two sections:
1. Configuration Section
The configuration section contains the information about your system on which your program is written and executed. This section must be specified in Area A.
It consists of two paragraphs:
- Source Computer: This will contain information about the system in which your program is compiled.
- Object Computer: This will contain information about the system in which your program is executed.
This Input-Output section contains information about all the files to be used in your COBOL program.
- File Control: This will show the external data set's information used in the program.
- I-O control: This will show the file's information used in the program.
Let's see one simple example for Environment section:
In COBOL, the data division is used to declare variables and parameters. Here, we declare the variables, their data type, size, usage type, etc. This is one of the most important divisions in the COBOL program structure.
Data division contains four sections:
- File section: This section is used to define the record structure of the file.
- Working-Storage section: This section is used for declaring temporary variables and file structures used in the program.
- Local-Storage section: This section is similar to Working-Storage section. It is only different in the terms that in this section, the variables are allocated and initialized every time when the program starts execution.
- Linkage section: This section is used to describe the data names received from an external program.
- Working-Storage Section: This section specifies all the temporary variables and also the file structures that are used in the program.
- Local-Storage Section: Local-storage section stores similar information as the working-storage section. The only difference is that in the case of the Local-Storage Section, the variables will be allocated and initialized every time a program starts execution.
- Linkage Section: This section describes all the data items received from an external program.
Let's see one simple example for Data Division:
- The procedure division is the main section of the COBOL program. Like the main() function of C/C++ program. From the procedure, division section program will actually start the execution.
- This section is the logical section of the program. Here all the business logic is written. Like C/C++ main() function, all COBOL programs must have a procedure division.
- The procedure division contains executable statements, and executable statements contain variables defined in the data division. We can use the user-defined names and paragraphs in the procedure division.
- We must specify at least one statement in this division. We cannot leave it empty.
- In this division, we write the main program code. It provides two ways to stop the execution of the program. In the case of calling program, we use STOP RUN, and in the case of called program, we use EXIT PROGRAM to stop the execution of the program.
Let's see the example for Procedure Division: