COBOL
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.
History
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.
Dialects
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.
COBOL courses
Three courses are planned, with one already released.
Syntax
COBOL Syntax is column dependent:
- 1-6 Sequence Number Area.
- 7 Indicator Area:
*
comment.-
continuation.D
debug line./
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.
Divisions
COBOL divides many elements of the program's definition into a series of ordered divisions. Within these we write sections, paragraphs, sentences, statements.
Identification division
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.
Environment division
The environment division defines the requirements of the host running the program, e.g. mapping files in the data division to data sources.
Configuration section
This optional section can be used to cross-compile programs for different computers and provide localisation settings.
Input/Output section
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.
File types
- 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.
Data division
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
Variables
PIC
(or PICTURE
) clauses follow the following format:
PIC <type>[(<length>)][V<PIC clause>]
Common types:
9
represents numericsA
represents text charactersX
represents 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).
Types
Define data types using the USAGE
clause:
01 SOME-VAR USAGE COMP1 VALUE 00.00
Numeric data types:
Data type | Implementation | Size | Comments |
---|---|---|---|
COMPUTATIONAL-1 COMP-1 | Short floating point | 4 bytes | Leftmost bit contains sign, next seven bits contain the exponent, remaining 3 bytes contain the mantissa. |
COMPUTATIONAL-2 COMP-2 | Long floating point | 8 bytes | Leftmost bit contains sign, next seven bits contain the exponent, remaining 7 bytes contain the mantissa. |
COMPUTATIONAL-3 COMP-3 , PACKED-DECIMAL | Packed-decimal | Variable | 1 byte for every two decimal places, except the right-most byte which contains 1 digit and the sign. |
COMPUTATIONAL-4 COMP-4 , COMP , BINARY | Fixed point | 2 (<= 4 digits), 4 (5-9 digits) or 8 (10-18 digits) bytes | Fixed point; leftmost bit contains sign. |
COMPUTATIONAL-5 COMP-5 | 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
Literals provide values that can be used as initial/constant values. Some figurative literals are built in:
ZERO
/ZEROES
SPACE
/SPACES
LOW-VALUE
HIGH-VALUE
NULL
/NULLS
File section
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.
Procedure division
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
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.
Loops
PERFORM VARYING COUNTER FROM 01 BY 1 UNTIL COUNTER EQUAL 11
END-PERFORM.
PERFORM A-PARAGRAPH COUNTER TIMES.
Program linkage
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 CALL
statement:
PROCEDURE DIVISION USING X, Y, Z
Statements
COMPUTE variable = operand1 operator operand2
stores the result of the operation invariable
.DISPLAY value
prints output to the console.FUNCTION name(arg1, arg2)
calls the intrinsic functionname
witharg1
andarg2
.GOBACK
jumps to the end of the subprogram if a subprogram (EXIT PROGRAM
), or the end of the program if the main program (STOP RUN
).MOVE operation TO variable
stores the result ofoperation
invariable
.STOP RUN
halts execution of the main program.
Intrinsic functions
- Math:
SUM
- Statistics:
- Date/time:
CURRENT-DATE
INTEGER-OF-DATE
DATE-OF-INTEGER
- Financial:
ANNUITY
- General:
LOWER-CASE
UPPER-CASE
Common operations
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
Backlinks