COBOL is the COmmon Business Oriented programming Language.
Even though it has become a pronounceable name, COBOL is still treated as an acronym by the standards committee, and COBOL is the preferred spelling by the ISO and INCITS standards bodies.
The current specification is
ISO/IEC 1989:2014 Information technology – Programming languages, their environments and system software interfaces – Programming language COBOL
That document was published in May of 2014 and can be purchased from various branches of standard bodies, officially homed at
http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detail.htm?csnumber=51416
Business oriented. That usually means transaction processing. Banking, government agencies, and the insurance industry are major areas of COBOL application deployments. IBM mainframe systems usually have a COBOL compiler installed. There are upwards of 300 COBOL dialects in existence, with perhaps 10 or so versions taking the lion's share of deployments. Most of these compilers are proprietary systems, but free software COBOL is also available.
COBOL is a procedural, imperative, compiled programming language. As of the COBOL 2002 spec, Object Oriented features were added to the standard.
By design intent, COBOL is a very verbose programming language. Although algebraic form is allowed:
COMPUTE I = R * B
the initial intent was to use full words for computational descriptions and data manipulation:
MULTIPLY INTEREST-RATE BY BALANCE GIVING CURRENT-INTEREST ROUNDED MODE IS NEAREST-EVEN
This design decision has both champions and detractors. Some feel it is too verbose, while others argue that the syntax allows for greater readability in a business environment.
COBOL is designed around decimal arithmetic, unlike most languages that use a binary internal representation. The COBOL spec calls for very precise fixed point decimal calculations, an aspect of the language that has been well regarded in financial sectors. COBOL also allows for USAGE BINARY, but leans towards decimal (base-10) representations.
COBOL dates back to the late 1950s, with initial implementations published in 1960.
U.S. Navy Rear Admiral Grace Hopper is often associated with COBOL, and championed on behalf of the language during the early stages of development. She was not the only person involved in the design and development of COBOL, by any means, but is often referred to as the Mother of COBOL.
Due to early backing by governments and large corporations, COBOL has been in wide use for many decades. It remains a point of pride for some, and a thorn for others, who see it as outdated. The truth likely lies somewhere in between these extreme views. When applied to transaction processing, COBOL is at home. When applied to modern web screens and networking applications it may not feel as comfortable.
COBOL programs are written in four separate divisions.
Being designed to handle decimal data, COBOL allows for PICTURE based data descriptions, in grouped hierarchies.
01 record-group.
05 balance pic s9(8)v99.
05 rate pic 999v999.
05 show-balance pic $Z(7)9.99.
That defines balance
as a signed eight digit value with two digits assumed after the decimal point. rate
is three digits before and three digits after an assumed decimal point. show-balance
is a numeric-edit field that will have a leading dollar sign, seven digits (zero suppressed) with at least one digit shown preceding two digits after a decimal point.
balance
can be used in calculations, show-balance
is only for display purposes and cannot be used in computational instructions.
COBOL is a reserved keyword heavy language. MOVE, COMPUTE, MULTIPLY, PERFORM style long form words make up most of the standard specification. Over 300 keywords and 47 operational statements in the COBOL 2014 spec. Many compiler implementations add even more to the reserved word list.
The CONTINUE statement causes the flow of control to continue at the next statement. Not quite a no-op, as it can influence control flow when inside compound statement sequences, in particular IF/THEN/ELSE.
A handy? example is during early development and building with and without debugging aids.
CALL "CBL_OC_DUMP" USING structure ON EXCEPTION CONTINUE END-CALL
That code, while expensive, will allow for formatted memory dumps when the module CBL_OC_DUMP
is linked into the executable, but will harmlessly fail when it is not. *That trick is only applicable during early stages of development. The expense of a dynamic lookup failure is not something to leave in active code, and those lines should be removed from the source as soon as any initial concerns are satisfied in alpha testing. On first day coding, it can be a handy aid. By second day coding ON EXCEPTION CONTINUE occurrences should be wiped clean.
The COBOL version of the C #include
preprocessor directive. Or, more historically accurate, COBOL came first, developed some 10 years earlier.
Due to some of the design decisions in COBOL (no arguments for PERFORM
as the primary reason), many data structure access sequences need to break the DRY principle. Names of structure components need to be repeated in the ENVIRONMENT DIVISION, the DATA DIVISION and possibly many times in the PROCEDURE DIVISION. This is usually handled by adding copybooks. Record declarations and access code are tucked away in separate files and the COPY statement is the only repeated source. A change to the copybook keeps all uses of name spelling and data layout in synch, instead of requiring multiple edits to multiple files when a change occurs.
The COBOL DIVIDE
statement divides one numeric item into others setting data items to the quotient and, optionally, the remainder.
ROUNDED
phrase:
Default is TRUNCATION
if no rounded phrase specified. Default ROUNDED
mode is NEAREST-TOWARD-ZERO
(rounding down) unless other specified. So called Banker's rounding is NEAREST-EVEN
.
The DISPLAY
statement causes data to be transferred to hardware or software of the operating environment. DISPLAY
comes in two forms, UPON device
or for display of SCREEN
data. Environment variables can also be set with DISPLAY UPON
in some implementations of COBOL, along with other extensions for data transfer of graphics or other device specific needs.
The COBOL EXIT
statement is a terminating flow control verb.
EXIT
comes is a few flavours:
EXIT
is a common end point for a series of procedures.EXIT PARAGRAPH
, EXIT SECTION
provides a means of exiting a structured procedure without executing any of the subsequent statements.EXIT FUNCTION
, EXIT METHOD
, EXIT PROGRAM
marks the logical end of a module of code.EXIT PERFORM
breaks out of a inline perform loop.EXIT PERFORM CYCLE
causes an inline perform loop to begin the next iteration.The conditional expression and selection statement. Use of explicit scope terminators is recommended. COBOL conditional expressions allow shortforms, where the current identifier (and conditional) is assumed through multiple condition tests, unless explicitly given.
IF A = 1 OR 2 ...
is equivalent to
IF A = 1 OR A = 2 ...
MOVE
is the workhorse of COBOL. Data is moved from literal or identifier to one or more identifiers. COBOL has a distinction between elementary and group MOVE. Elementary data is type converted from source to destination. Group data is moved as a byte array, without regard to field types with a structure. Numeric fields are moved from right to left, high order digit truncation with zero fill (normally). Alphanumeric character data is moved left to right, right end character truncation with space fill. There are quite a few rules on how MOVE
goes about its business, with both BINARY and PICTURE DISPLAY data forms, and group hierarchies all accounted for.
The COBOL OPEN
statement initiates file processing. File resources in COBOL are defined in the ENVIRONMENT DIVISION
, named in FD
(File Descriptor) paragraphs. These fd names are used to access physical disk files and various options are specified in a SELECT
clauses in the FILE-CONTROL
paragraph of the INPUT-OUTPUT SECTION
. A programmer is expected to test a FILE STATUS
identifier for status and error codes.
Modes include INPUT
, OUTPUT
, I-O
and EXTEND
.
The PERFORM statement transfers control to one or more procedures and returns control implicitly when the sequence completes. PERFORM can also be used for inline loops withing the scope of the PERFORM.
The VARYING
phrase allows for nesting with one or more AFTER
clauses, and the conditional test can be BEFORE
(default) or AFTER
each loop.
The THRU
clause of a procedural perform assumes sequential top down control flow from procedure-1
through the end of procedure-2
. THRU is a contentious issue, and many programmers prefer PERFORM
by SECTION
rather than using THRU
paragraphs. Some shops may mandate PERFORM THRU
with an explicit exit point paragraph, others may ban the use of THRU
finding it more difficult to debug.
Procedural perform:
Inline perform:
Where varying-phrase
is:
The READ
statement is a staple of COBOL transaction processing programming. Reads data from external storage into working store. With or without locks or sharing, sequentially, by random access, or by key. Declarative clauses for AT END
may also be specified, but some programmers prefer explicit FILE STATUS
testing.
As each file resource may contain any type of record in any given slot, COBOL is a "read a file", "write a record" language, READ
takes a filename (FD) and it is up to the programmer to place the record in an appropriate structure if heterogeneous data is saved in the file.
The START
statement provides a way to position a read in a file for subsequent sequential retrieval (by key).
The key relational can include (but is not limited to):
KEY IS GREATER THAN
KEY IS >
KEY IS LESS THAN
KEY IS <
KEY IS EQUAL TO
KEY IS =
KEY IS NOT GREATER THAN
KEY IS NOT >
KEY IS NOT LESS THAN
KEY IS NOT <
KEY IS NOT EQUAL TO
KEY IS NOT =
KEY IS <>
KEY IS GREATER THAN OR EQUAL TO
KEY IS >=
KEY IS LESS THAN OR EQUAL TO
KEY IS <=
The STOP
statement terminates the current run unit.
A now deemed obsolete extension to STOP RUN
is STOP literal
, which will pause a program until a response from the console is given, at which point execution will resume. This could be handy for things like, "go get the big box of paper and load up the special printer".
STOP
is a hard program end, GOBACK
is a slightly nicer way of returning to the operating system or caller module, especially in subroutines that may have no business terminating a run.
COBOL 2014 lists the following standard Intrinsic Functions:
======================================== ==========
Intrinsic Function Parameters
======================================== ==========
FUNCTION ABS 1
FUNCTION ACOS 1
FUNCTION ANNUITY 2
FUNCTION ASIN 1
FUNCTION ATAN 1
FUNCTION BOOLEAN-OF-INTEGER 2
FUNCTION BYTE-LENGTH 1
FUNCTION CHAR 1
FUNCTION CHAR-NATIONAL 1
FUNCTION COMBINED-DATETIME 2
FUNCTION COS 1
FUNCTION CURRENCY-SYMBOL 0
FUNCTION CURRENT-DATE 0
FUNCTION DATE-OF-INTEGER 1
FUNCTION DATE-TO-YYYYMMDD Variable
FUNCTION DAY-OF-INTEGER 1
FUNCTION DAY-TO-YYYYDDD Variable
FUNCTION DISPLAY-OF Variable
FUNCTION E 0
FUNCTION EXCEPTION-FILE 0
FUNCTION EXCEPTION-FILE-N 0
FUNCTION EXCEPTION-LOCATION 0
FUNCTION EXCEPTION-LOCATION-N 0
FUNCTION EXCEPTION-STATEMENT 0
FUNCTION EXCEPTION-STATUS 0
FUNCTION EXP 1
FUNCTION EXP10 1
FUNCTION FACTORIAL 1
FUNCTION FORMATTED-CURRENT-DATE 1
FUNCTION FORMATTED-DATE 2
FUNCTION FORMATTED-DATETIME Variable
FUNCTION FORMATTED-TIME Variable
FUNCTION FRACTION-PART 1
FUNCTION HIGHEST-ALGEBRAIC 1
FUNCTION INTEGER 1
FUNCTION INTEGER-OF-BOOLEAN 1
FUNCTION INTEGER-OF-DATE 1
FUNCTION INTEGER-OF-DAY 1
FUNCTION INTEGER-OF-FORMATTED-DATE 2
FUNCTION INTEGER-PART 1
FUNCTION LENGTH 1
FUNCTION LENGTH-AN 1
FUNCTION LOCALE-COMPARE Variable
FUNCTION LOCALE-DATE 2
FUNCTION LOCALE-TIME 2
FUNCTION LOCALE-TIME-FROM-SECONDS 2
FUNCTION LOG 1
FUNCTION LOG10 1
FUNCTION LOWER-CASE 1
FUNCTION LOWEST-ALGEBRAIC 1
FUNCTION MAX Variable
FUNCTION MEAN Variable
FUNCTION MEDIAN Variable
FUNCTION MIDRANGE Variable
FUNCTION MIN Variable
FUNCTION MOD 2
FUNCTION MODULE-CALLER-ID 0
FUNCTION MODULE-DATE 0
FUNCTION MODULE-FORMATTED-DATE 0
FUNCTION MODULE-ID 0
FUNCTION MODULE-PATH 0
FUNCTION MODULE-SOURCE 0
FUNCTION MODULE-TIME 0
FUNCTION MONETARY-DECIMAL-POINT 0
FUNCTION MONETARY-THOUSANDS-SEPARATOR 0
FUNCTION NATIONAL-OF Variable
FUNCTION NUMERIC-DECIMAL-POINT 0
FUNCTION NUMERIC-THOUSANDS-SEPARATOR 0
FUNCTION NUMVAL 1
FUNCTION NUMVAL-C 2
FUNCTION NUMVAL-F 1
FUNCTION ORD 1
FUNCTION ORD-MAX Variable
FUNCTION ORD-MIN Variable
FUNCTION PI 0
FUNCTION PRESENT-VALUE Variable
FUNCTION RANDOM Variable
FUNCTION RANGE Variable
FUNCTION REM 2
FUNCTION REVERSE 1
FUNCTION SECONDS-FROM-FORMATTED-TIME 2
FUNCTION SECONDS-PAST-MIDNIGHT 0
FUNCTION SIGN 1
FUNCTION SIN 1
FUNCTION SQRT 1
FUNCTION STANDARD-COMPARE Variable
FUNCTION STANDARD-DEVIATION Variable
FUNCTION STORED-CHAR-LENGTH 1
FUNCTION SUM Variable
FUNCTION TAN 1
FUNCTION TEST-DATE-YYYYMMDD 1
FUNCTION TEST-DAY-YYYYDDD 1
FUNCTION TEST-FORMATTED-DATETIME 2
FUNCTION TEST-NUMVAL 1
FUNCTION TEST-NUMVAL-C 2
FUNCTION TEST-NUMVAL-F 1
FUNCTION TRIM 2
FUNCTION UPPER-CASE 1
FUNCTION VARIANCE Variable
FUNCTION WHEN-COMPILED 0
FUNCTION YEAR-TO-YYYY Variable
======================================== ==========
GnuCOBOL adds
======================================== ==========
FUNCTION CONCATENATE Variable
FUNCTION SUBSTITUTE Variable
FUNCTION SUBSTITUTE-CASE Variable
======================================== ==========
The keyword FUNCTION
is required unless source (or compile time option)
includes
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC.
Where ALL INTRINSIC
can be a list of functions to be used without the
FUNCTION
prefix in PROCEDURE DIVISION
statements.
The LENGTH
function has a sorted history. Some compilers include a LENGTH
reserved word. For GnuCOBOL, this reserved word is only recognized when used
in the phrase LENGTH OF
, the OF
token is required to disambiguate the
function from the older reserved word extension.
The USE
statement specifies procedures to be used
Obsolete usage includes specifying procedures to be used during DEBUGGING
, and extensions include adding interstitial procedures for program start and end.