cobol

Topics related to cobol:

Getting started with cobol

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.

Standard Specification

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

Principal field of use

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.

Category

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.

Decimal Math

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.

History

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.

Structure

COBOL programs are written in four separate divisions.

  • IDENTIFICATION DIVISION
  • ENVIRONMENT DIVISION
  • DATA DIVISION
  • PROCEDURE DIVISION

Data Descriptions

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.

Procedural statements

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.

GnuCOBOL installation with GNU/Linux

ACCEPT statement

ADD statement

enter image description here

Where rounded-phase is

enter image description here

ALLOCATE statement

ALTER statement

CANCEL statement

CALL statement

COMMIT statement

enter image description here

Flushes ALL current locks, synching file I/O buffers.

This is a non standard extension, available with some COBOL implementations that support ROLLBACK features.

COMPUTE statement

CONTINUE statement

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.

enter image description here

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.

COPY directive

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.

enter image description here

String

DELETE statement

DIVIDE statement

The COBOL DIVIDE statement divides one numeric item into others setting data items to the quotient and, optionally, the remainder.

enter image description here

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.

ROUNDED phrase

DISPLAY statement

EVALUATE statement

EXIT statement

GENERATE statement

FREE statement

GO TO statement

GOBACK statement

IF statement

INITIALIZE statement

INITIATE statement

INSPECT statement

The INSPECT statement is a scan and replace verb in COBOL.

INSPECT statement syntac diagram

Where tallying-phrase is:

tallying-phrase

replacing-phrase is:

missing image

before-after-phrase is:

before-after-phrase

MERGE statement

MOVE statement

MULTIPLY statement

OPEN statement

PERFORM statement

READ statement

RELEASE statement

RETURN statement

REPLACE directive

REWRITE statement

SET statement

SEARCH statement

SORT statement

START statement

The START statement provides a way to position a read in a file for subsequent sequential retrieval (by key).

START statement syntax diagram

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 <=

SUBTRACT statement

STOP statement

TERMINATE statement

STRING statement

SUPPRESS statement

UNLOCK statement

Intrinsic Functions

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.

UNSTRING statement

USE statement

WRITE statement

Data division

How does the computational work in cobol?