This is just an overview. For complete details, consult the appropriate IBM manuals. There are more options than are listed here, and older compilers may require some items now not mandatory.
4 Divisions of a COBOL program
This provides identifying information about the program. It must include:
Program-ID
and may also include some other information (all optional):
Author
Installation
Date-Written
Date-Compiled
Security
This depends on the specific computer on which we are working. Some of the details of the format will vary accordingly. For instance, if we are using VSAM, the Select-Assign statements become more complex.
This contains two sections, both technically optional:
File-Control.
Select-Assign statements
Source-Computer
Object-Computer
This describes the data items in a program. It contains several sections, each tchenically optional:
File Section
Working-Storage Section
Linkage Section
Local-Storage Section
The File Section describes the files in use, along with the data record for those files. It is needed if the program uses any files at all.
The Working-Storage Section defines and perhaps initializes the variables. It is needed if the program has any variables at all.
The Linkage Section describes parameters used when the program is called by another program (and may contain other item as well). It is needed if the program receives a PARM.
The Local-Storage Section is used in recursion and is not needed otherwise.
This contains all the executable code, normally organized in a main routine and assorted internal subroutines.
Example
Identification Division.
Program-ID. Program-Name.
Author. Your Name.
Date-Compiled. (Supplied by the system.)
Environment Division.
Configuration Section.
Source-Computer. IBM-390.
Object-Computer. IBM-390.
Input-Output Section.
File-Control.
Select File-Name Assign To DDname.
Data Division.
File Section.
FD File-Name
Recording Mode is F.
01 Record-Name.
* (definition of Record-Name)
Working-Storage Section.
* (definitions of various items)
Linkage Section.
* (descriptions of parameters)
Procedure Division.
000-Main.
* (assorted code)
Stop Run.
100-Sub.
* (code)
Let's look at the above example.
In the Procedure Division, execution will begin with the first line of code, and it ends when the Stop Run command is executed. We normally have just one Stop Run in a program.
This example has no documentation (other than the parenthetical remarks). Normally, we do require documentation in a COBOL program.
Page layout
COBOL code is written in 80-column lines. We leave columns 1-6 blank, we usually leave column 7 blank, and we always leave columns 73-80 blank. Columns 8-11 are the A margin, and columns 12-72 are the B margin.
Column 7 is used in two ways: put an asterisk (*) in column 7 to make the line into a comment, or put a dash (-) in column 7 to indicate continuation of a character literal to the next line.
Some items must begin in the A margin: names of divisions, names of sections, names of subroutines, 01-level items in the Data Division, etc.