COBOL, the Common Business-Oriented Language, is a structured, imperative, procedural and OOP (since 2002) programming language predominantly used for business, finance and administrative tasks.
The language was developed by the US DoD and private sector to reduce operational costs of computing caused by poor interoperability, which meant new hardware changes brought new languages, warranting retraining. IBM dropped COMTRAN as the DoD forced COBOL's inclusion.
It's based upon the FLOW-MATIC, formerly B-0 (Business Language version 0) data processing language developed for the UNIVAC I at Remington Rand under Grace Hopper.
The language has stuck around because it's backwards compatible and well optimised for batch jobs.
Due to the flexible nature of the specification and subsequent revisions many permutations of COBOL have come to exist. Some notable ones:
- Enterprise COBOL is designed for IBM Z mainframes.
Three courses are planned, with one already released.
COBOL Syntax is column dependent:
- 1-6 Sequence Number Area.
- 7 Indicator Area:
/for source code listing format.
- 8-11 A Area divisions, sections, paragraphs, level indicators; all elements giving it structure.
- 12-72 B Area statements, sentences, clauses.
- 73-80 Identification Area ignored by the compiler, used by the program for any purpose.
COBOL divides many elements of the program's definition into a series of ordered divisions. Within these we write sections, paragraphs, sentences, statements.
The identification division provides metadata about the program. The
PROGRAM-ID paragraph is required and must be specified before all other paragraphs.
IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. AUTHOR. LUKE CARRIER. INSTALLATION. LEARN VSCODE. DATE-WRITTEN. 30/09/2020. DATE-COMPILED. 30/09/2020 09:09:24. SECURITY. NON-CONFIDENTIAL.
The environment division defines the requirements of the host running the program, e.g. mapping files in the data division to data sources.
This optional section can be used to cross-compile programs for different computers and provide localisation settings.
The input/output section defines relationships between COBOL internal filename and external data set names.
ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT internal-file-name ASSIGN external-file-name.
- Flat files provide storage of data as a series of records, with no delimiter. Variable length records are facilitate with RLE.
- VSAM files build on this incrementally:
- ESDS enabled record deletion.
- RRDS allows retrieval using RRN.
- KSDS allows random reads based on in-record data using that keyed sequence. Keys can span multiple records providing the key is contiguous.
The Data division contains both variables and literals (constants) which are referred to by data names. Data names can contain most characters up to a length of 30 characters, excluding spaces and reserved words.
Types can represent symbols which appear in the value, e.g.
PIC $9,999.99 might represent USD values:
DATA DIVISION. WORKING-STORAGE SECTION. 77 NAME PIC X(30).
FILLER can be used to declare spacers:
01 HEADER-1 05 FILLER PIC X(20) 'Financial report for'. 05 FILLER PIC X(60) VALUE SPACES
PICTURE) clauses follow the following format:
PIC <type>[(<length>)][V<PIC clause>]
Arepresents text characters
Xrepresents alphanumeric characters
Variables can be groups for other variables:
01 CARD-NUMBER. 05 CARD-SEMENT-1 PIC 9(4). 05 DIVIDER-1 PIC X VALUE SPACE. 05 CARD-SEMENT-2 PIC 9(4). 05 DIVIDER-2 PIC X VALUE SPACE. 05 CARD-SEMENT-3 PIC 9(4). 05 DIVIDER-3 PIC X VALUE SPACE. 05 CARD-SEMENT-4 PIC 9(4).
Define data types using the
01 SOME-VAR USAGE COMP1 VALUE 00.00
Numeric data types:
|Short floating point||4 bytes||Leftmost bit contains sign, next seven bits contain the exponent, remaining 3 bytes contain the mantissa.|
|Long floating point||8 bytes||Leftmost bit contains sign, next seven bits contain the exponent, remaining 7 bytes contain the mantissa.|
|Packed-decimal||Variable||1 byte for every two decimal places, except the right-most byte which contains 1 digit and the sign.|
|Fixed point||2 (<= 4 digits), 4 (5-9 digits) or 8 (10-18 digits) bytes||Fixed point; leftmost bit contains sign.|
|Floating point||2, 4 or 8 bytes||Always has a sign bit.|
Multiple character encodings exist; some common ones:
- EBCDIC is a legacy encoding, but you'll likely still encounter it in some datasets, particularly older ones.
- ASCII is most commonly used.
Literals provide values that can be used as initial/constant values. Some figurative literals are built in:
The file section contains a number of
FD paragraphs which describe the structure of input and output files. These filenames must match the
SELECT...ASSIGN sentences in the environment division.
DATA DIVISION. FILE SECTION. FD internal-file-name RECORDING MODE F. 01 PRINT-REC. 05 FIRST-NAME PIC X(20). 05 LAST-NAME PIC X(20).
Working storage section
This section defines data records used during the program's execution.
There are two forms of scope terminator:
- The implicit terminator (
.) ends the scope of all statements not yet ended.
- Explicit terminators (e.g.
END-IF) end a verb without ending a sentence.
PROCEDURE DIVISION. DISPLAY 'HELLO WORLD!'. STOP RUN.
Open files section
PROCEDURE DIVISION. OPEN-FILES. OPEN INPUT x OPEN OUTPUT y
Close stop section
CLOSE x. CLOSE y. STOP RUN.
Paragraphs allow declaring subroutines that can be reused. Like functions in other languages, getting this structure right is worth some time.
Execute paragraphs in sequence in order of definition:
PERFORM THIS-IS THRU A-BAD-IDEA.
PERFORM VARYING COUNTER FROM 01 BY 1 UNTIL COUNTER EQUAL 11 END-PERFORM. PERFORM A-PARAGRAPH COUNTER TIMES.
Linkage allows calls between COBOL programs, with the program name specified by either a string literal or a variable:
CALL 'PROGRAM' MOVE 'PROGRAM' TO PROGRAM-NAME CALL PROGRAM-NAME
The data division's
LINKAGE SECTION declares variables that the callee will receive from the calling program. The procedure division must include a
USING clause which accepts the arguments in the order they're passed in the caller's
PROCEDURE DIVISION USING X, Y, Z
COMPUTE variable = operand1 operator operand2stores the result of the operation in
DISPLAY valueprints output to the console.
FUNCTION name(arg1, arg2)calls the intrinsic function
GOBACKjumps to the end of the subprogram if a subprogram (
EXIT PROGRAM), or the end of the program if the main program (
MOVE operation TO variablestores the result of
STOP RUNhalts execution of the main program.
Looping over records
The C major of COBOL programming
READ-NEXT-RECORD. PERFORM READ-RECORD PERFORM UNTIL LASTREC = 'Y' PERFORM WRITE-RECORD PERFORM READ-RECORD . * READ-RECORD. READ internal-file-name AT END MOVE 'Y' TO LASTREC END-READ. * WRITE-RECORD. MOVE some-field TO some-field-o. WRITE internal-file-name-o