Software Engineers Handbook/Language Dictionary/COBOL

COBOL
COBOL is a computer programming language whose name is an acronym for "COmmon Business-Oriented Language". It is widely used in the financial services industry typically for mainframe based applications. It has been used extensively since the 1960s. COBOL was designed for use in business applications and consequently it was implemented with fixed-point arithmetic support. This is one of the advantages of COBOL as it allows complete and accurate computation of financial values without the possibility of rounding errors.

Type
COBOL is a full procedural language.

Execution Entry Point
A COBOL program is split into four divisions, all four divisions must be present in every program in this sequence: Identification; Environment; Data and Procedure. Each division contains a number of mandatory and optional sections and paragraphs.

The Identification Division contains information about the program, including its name, author, date of compilation, etc. It must contain the  paragraph that uniquely names the program. All other paragraphs are optional.

The Environment Division describes the environment in which the program will execute. For example, it includes details of input and output files and the target execution platform. The division contains two sections: the Configuration Section and the Input-Output section.

The Data Division contains the File Section, which fully describes the record layout of each input and output file, and the Working-Storage Section where variables are declared. It also contains the Linkage Section that describes data that will be passed between programs that call each other.

The Procedure Division contains the executable program instructions. The execution entry point is the first statement in the division.

General Syntax
It would be very difficult to give an overview of COBOL's general syntax, since the language has over 500 reserved words. Moreover, the syntax (like other early languages, including FORTRAN) is not designed to follow a formal grammar; for this reason COBOL and FORTRAN parsers are rarely implemented using automatic code generators.

Here are some sample assignment statements:

While many sites still use all uppercase characters, modern COBOL compilers are case-insensitive. The following two statements are absolutely equivalent:

Character case is respected for quoted literals (character strings).

Comments
Normally the only way to specify a comment is by putting an asterisk in the indicator-area (column 7). The entire line is a comment. Block comments must have * in column 7 of every line. There is a 2002 standard that allows part of a line to be a comment:

Variable Declarations
There are a few variables of a type known as special registers that do not need to be defined, but generally all variables must be defined in COBOL, unlike interpreted BASIC.

COBOL has a number of data types, but the most common are numeric (with specifiers for internal format), numeric edited data, alphanumeric data, and records (similar to structs in C):

Your simple example, declaring an integer, might look something like this:

COBOL also supports redefinition, which is somewhat similar to union in C.

Method Declaration/Implementation
Object-oriented extensions are available to COBOL, but the vast majority of COBOL programmers do not make use of them, and see no need for them.

However, if a COBOL programer wants to create a class, it is quite easy. For example, here is a HelloWorld class with an instanceMethod

Scope
All data contained in the working-storage section of the COBOL program is global. Some data items may be defined as external, and visible to other programs in a load module. The data items in a COBOL subprogram are global to the subprogram, but local since the caller cannot see them. Typically, data is "passed" from a COBOL main program to a subprogram by reference. There are alternatives of "by content" or "by value", but they are typically only used to call subprograms written in C or other languages that do not normally use call by reference.

Some vendors provide extensions, such as thread-local-storage section and local-storage section. These extensions allow data to be scoped as either thread-local or temporary to the program.

Conditional statements
There are a lot of them. Here are some examples:

Looping Statements
There are several of those too:

Output Statements
Here are several examples.

For key-sequenced files, there is support for,  ,  ,  , and.

Containers
Not applicable to COBOL.

Algorithms
Recursion is not generally supported, although it can be simulated by iteration.

Garbage collection
Memory allocated dynamically via system APIs such as malloc should be free'ed with the appriopiate call to "free".

COBOL programs that uses CLASS'es may need to invoke the "finalize" method to release the object. For example:

If the COBOL application is executing under a VM such as a Java VMs or Microsoft's CLR, standard garbage collection rules apply.

Otherwise..

COBOL programmers do not normally know or care what garbage collection is. They rarely have to allocate memory or clean it up afterwards.

Physical Structure
A COBOL source file may have (but need not have) copybooks, somewhat similar to .h files in C. The compiled program generally makes use of a COBOL runtime library of service routines that are dynamically loaded. The COBOL source file itself consists of four divisions: ,  ,  , and.

Tips


Web references

 * COBOL FAQ (as .DOC)
 * GnuCOBOL FAQ and How-To
 * GnuCOBOL Manuals and Guides
 * COBOL User Groups (COBUG) (site offline, archive from 2013)
 * cob-rte
 * svalgard
 * swbell page
 * dissert
 * csis.ul.ie page
 * infogoal page
 * COBOL Blog

Books and articles


Back to the title page

Tools

 * Cobos Project The Open Source Cobol / Mainframe / CICS / DB2 Integrated Development Environment (within Eclipse)

Programmation Cobol Programmeren in COBOL