Software Engineers Handbook/Language Dictionary/COBOL

COBOL

edit

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

edit

COBOL is a full procedural language.

Execution Entry Point

edit

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 PROGRAM-ID 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

edit

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:

 MOVE A TO B.
 COMPUTE GROSS-PAY = HOURS-WORKED * HOURLY-RATE.
 MULTIPLY HOURLY-RATE BY HOURS-WORKED GIVING GROSS-PAY.
 SET MY-INDEX UP BY 1.

 SET ADDRESS OF MY-LINKAGE-SECTION-ITEM TO MY-POINTER.
 READ TRANSACTION-FILE INTO TRANSACTION-RECORD-WS.

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

 MOVE MY-NUMBER TO YOUR-NUMBER.
 Move my-number to Your-Number.

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

Comments

edit

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:

 MOVE A TO B.   *> THIS IS A COMMENT ON THE SAME LINE AS A STATEMENT

Variable Declarations

edit

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):

 01  TRANSACTION-RECORD.
    05  RECORD-NUMBER            PICTURE S9(7) COMP-3 VALUE ZERO.
    05  RECORD-DESCRIPTION       PICTURE X(30) VALUE SPACES.
    05  EDITED-AMOUNT            PIC $$$$,$$$,$$$.99-.
    05  FILLER                   PIC X(60) VALUE SPACES.

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

 77  I    pic s9(4)  usage is binary.

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

Method Declaration/Implementation

edit

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

 IDENTIFICATION DIVISION.
 CLASS-ID. HelloWorld.
 ENVIRONMENT DIVISION.
 CONFIGURATION SECTION.
 REPOSITORY.
 
 IDENTIFICATION DIVISION.
 OBJECT.
 DATA DIVISION.
 WORKING-STORAGE SECTION.
 
 IDENTIFICATION DIVISION.
 METHOD-ID. instanceMethod.
 DATA DIVISION.
 LINKAGE SECTION.
 PROCEDURE DIVISION.
     DISPLAY "Hello World"
     GOBACK.
 END METHOD instanceMethod.
 
 END OBJECT.
 END CLASS HelloWorld.

Scope

edit

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

edit

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

 If my-number is numeric
    continue
 else
    display 'data field "my-number" is not numeric'
 end-if

 evaluate record-type
 when 'a'
    perform process-record-type-a
 when 'b'
    perform process-record-type-b
 when 'c'
    continue
 when 'd'
 when 'e'
    perform process-record-type-d-or-e
 when other
    perform process-invalid-record-type
 end-evaluate

 READ TRANSACTION-FILE INTO MY-TRAN-REC-WS
 AT END
    SET ALL-RECORDS-PROCESSED TO TRUE
 NOT AT END
    ADD +1 TO TRAN-REC-COUNT
    PERFORM PROCESS-1-TRANSACTION
 END-READ

Looping Statements

edit

There are several of those too:

 Perform 6 times
    add +1 to loop-count
 end-perform

 Perform process-1-billing-record
    until all-records-processed

 Perform clear-1-table-entry
    varying tbl-index from +1 by +1
    until tbl-index is greater than max-table-entries

 Search table-entry varying tbl-index
 at end
    set entry-not-found to true
 when table-key equal customer-id
    move tbl-customer-name to print-customer-name
 end-search

Output Statements

edit

Here are several examples.

 DISPLAY "Hello, World!"
 display 'hello, world!' with no advancing

 Write billing-record from transaction-record-ws

For key-sequenced files, there is support for START, READ, WRITE, REWRITE, and DELETE.

Containers

edit

Not applicable to COBOL.

Algorithms

edit

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

Garbage collection

edit

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:

 invoke myObject "finalize" returning myObject

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

edit

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: IDENTIFICATION DIVISION, ENVIRONMENT DIVISION, DATA DIVISION, and PROCEDURE DIVISION.

Tips

edit

<Please include tips that make it easier to switch to this language from another language.>

Web references

edit

Books and articles

edit

<List additional books and articles that may be helpful. Please include for what level reader the references are appropriate. (beginner/intermediate/advanced)>

Back to the title page

Tools

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