COBOL is a computer programming language whose name is an acronym formed by the upper-case letters of the words "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.
COBOL is a full procedural language.
Execution Entry PointEdit
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 which 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 which 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.
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 TO 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).
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
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.
Object-oriented extensions are available to COBOL, but the vast majority of COBOL programmers don't know 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 an HelloWorld class with an InstanceMethod!
class-id. HelloWorld as "HelloWorld". environment division. configuration section. repository. object. data division. working-storage section. method-id. "InstanceMethod". local-storage section. linkage section. procedure division. display "Hello World" exit method. end method "InstanceMethod". end object. end class HelloWorld
If the COBOL programmer wants to interact with external classes such as the .NET Framework it can do too. For example here is a example of obtaining the current date/time from .NET.
repository. class sys-datetime as "System.DateTime" . 01 now sys-datetime. procedure division. set now to sys-datetime::"Now" display now
All data contained 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 scoped as either thread-local or temporary to the program.
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
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
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
Not applicable to COBOL.
Recursion is not generally supported, although it can be simulated by iteration.
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.
COBOL programmers do not normally know or care what garbage collection is. They rarely have to allocate memory or clean it up afterwards.
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:
DATA DIVISION, and
<Please include tips that make it easier to switch to this language from another language.>
- COBOL FAQ
- COBOL User Groups (COBUG)
- swbell page
- csis.ul.ie page
- infogoal page
- COBOL Blog
Books and ArticlesEdit
<List additional books and articles that may be helpful. Please include for what level reader the references are appropriate. (beginner/intermediate/advanced)>
- Cobos Project The Open Source Cobol / Mainframe / CICS / DB2 Integrated Development Environment (within Eclipse)