DIVISIONS in COBOL

The following are the Four Divisions of COBOL :

COBOL program code is divided into four basic division:
IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE divisions. The
identification division is required, but in theory the others are not
absolute (although you won't have much of a program without any
procedures or data!).

=======================
The IDENTIFICATION DIVISION
=======================

The identification division tells the computer the name of the program
and supplies other documentation concerning the program's author, when
it was written, when it was compiled, who it is intended for...etc. In
fact, only the program name is required by the compiler.

Syntax:-

000100 IDENTIFICATION DIVISION.
000110 PROGRAM-ID. EXAMPLE-1-PROG.
000120 AUTHOR. ZINGMATTER.
000130 INSTALLATION. XYZ GROUP.
000140 DATE-WRITTEN. 17/5/00.
000150 DATE-COMPILED.
000160 SECURITY. LOCAL GROUP.

Note:

* The use of full stops is important. Throughout these pages, note
where they are positioned

* The first words (PROGRAM-ID, AUTHOR etc..) are written in area A,
the details are in
area B

* The DATE-COMPILED detail is written automatically by the compiler


=======================
The Environment Division
=======================
Syntax :-
000260 ENVIRONMENT DIVISION.
000270 CONFIGURATION SECTION.
000280 SOURCE-COMPUTER. IBM PC.
000290 OBJECT-COMPUTER. IBM PC.
000300 INPUT-OUTPUT SECTION.

000310 FILE-CONTROL.
000320 SELECT INPUT-FILE ASSIGN TO 'input.dat'
000330 ORGANIZATION IS LINE SEQUENTIAL.
000340 SELECT PRINT-FILE ASSIGN TO PRINTER.

Notes :-
* You probably wouldn't need to bother with the configuration section.
* The DIVISION and SECTION words are written into area A but the SELECT
clause should be in area B.
* The full stop doesn't appear in the SELECT clause until after the
ORGANIZATION
has been specified.
* INPUT-FILE and PRINT-FILE are user-defined names that are used in
the program
to refer to 'input.dat' and the printer, respectively. If the
input.dat file was on a different disk drive, within a directory
structure, then you could write:
...ASSIGN TO 'D:datafiles/data/input.dat'.
* Line 000330 describes the structure or form of the data written in
'input.dat' file. In this case, each record is on a new line in the
file (see
* File Handling section for details). The printer also is assigned
but the organization doesn't have to be secified.
* For the SELECT clause, if no organization is defined the computer
defaults to SEQUENTIAL organization (i.e. each record appears in a
long string with no line breaks.


=======================
The Data Division
=======================

The data division is where memory space in the computer is allocated
to data and identifiers that are to be used by the program.

Two important sections of this division are the FILE SECTION and the
WORKING-STORAGE SECTION. The file section is used to define the
structure, size and type of the data that will be read from or written
to a file.

Suppose the 'input.dat' file (described above) contains a series of
records about a companies customers, giving details of name, address,
and customer number. If you were to open 'input.dat' with a text
editor you would see each record on a new line like this:

Joe Bloggs 20Shelly Road Bigtown 023320
John Dow 15Keats Avenue Nowheresville042101
Jock MacDoon05Elliot Drive Midwich 100230

etc...


The different pieces of data need to be defined so that the program
can read a record at a time, placing each piece of information into
the right area of memory (which will be labelled by an identifier).

The file section for this may look like this:

000400 DATA DIVISION.
000410 FILE SECTION.
000420
000430 FD INPUT-FILE.
000440 01 CUSTOMER-DATA.
000450 03 NAME PIC X(12).
000460 03 ADDRESS.
000470 05 HOUSE-NUMBER PIC 99.
000480 05 STREET PIC X(19).
000490 05 CITY PIC X(13).
000500 03 CUST-NUMBER PIC 9(6).


Notes:

* 'FD' stands for File Descriptor, and names the file, INPUT-FILE
(assigned in the environment division), and describes the exact
structure of the data in each record. All records in this file MUST be
of exactly the same structure.

* '01 CUSTOMER-DATA' is the group name and refers to all of the
single record that is read into the computer memory from the file. The
higher numbers (levels), 03.. and 05.. will contain the indivual
fields of the record.

* Both FD and 01 are written in area A while higher levels are in area B.
* Level 01 is sub-grouped into level 03 fields. Notice that one of
the level 03 sub-groups is itself sub-grouped into level 05. The
sub-grouping could continue upwards as required to 07, 09 etc.. These
numbers (except level 01) could as easily be 02, 03, 04 ...or any
increasing number scale. There are some numbers (i.e. 66, 77 and 88)
which actually have other uses but these will be discussed in the
Defining Data section.

* The PIC (short for PICTURE) clause indicates the size and type of
data that that field contains. For example, in line 000450, the data
name (identifier) NAME has been defined as holding 12 characters of
alphnumeric data. It could have been written as PIC XXXXXXXXXXXX be
that's a pain. 'X' means alphnumeric and can contain any ASCII
character. However, even if it contained '2' you could not do any
calculations on this as the information is stored as the ASCII code
for the character '2', rather than the actual number 2. Line 000470
defines HOUSE-NUMBER as PIC 9(2), which can hold a 2-digit number.

You can do calculations with this since '9' is used to denote a numeric field.


* Notice how the group names (CUSTOMER-DATA and ADDRESS) do not have
PIC descriptions. This is because the higher level field descriptions
when added together will be the size of the group name, i.e.
CUSTOMER-NUMBER will hold 46 characters which turns out to be the size
of each record (spaces are included). You can refer to these group
names but when doing so all data will be treated as alphanumeric and
cannot be used for calculations, even if all of the higher group items
are numeric.


The WORKING-STORAGE SECTION of the data division is for defining data
that is to be stored in temporary memory, i.e. during program
run-time. Effectively, this is where, for example, an identifier is
defined that will hold the result of a calculation.

000500 DATA DIVISION.
000510 WORKING-STORAGE SECTION.
000520
000530 01 RECORD-COUNTER PIC 9(5).

Also see the 'Hello World program. In that case the string to be
displayed on the screen is actually defined in working-storage using
the VALUE clause (01 TEXT-OUT PIC X(12) VALUE 'Hello World!'). The
same can be done for numeric data e.g.:


000800 01 TOTALS-IN.
000810 03 1ST-NO PIC 99 VALUE ZERO.
000820 03 2ND-NO PIC 999 VALUE 100.


The equivalent to filling an item such as 1ST-NO (above) with zeroes,
is filling an alphanumeric (PIC X) item with spaces e.g. 01 MESSAGE
PIC X(12) VALUE SPACES.


=======================
The Procedure Division
=======================
COBOL is a modular language, in that a program is usually broken up
into units described as paragraphs.


000900 PROCEDURE DIVISION.
000910 CONTROL-PARAGRAPH.
000920 PERFORM READ-DATA-FILE
000930 PERFORM CALULATE-PRICES
000940 PERFORM PRINT-PRICE-REPORT
000950 STOP RUN.

The PERFORM statement is used to 'call' other paragraphs to do each
task. These paragraphs would appear in the same coding and are part of
the same program. In the above example, the program would consist of
four paragraphs: the CONTROL-PARAGRAPH and the three called from
within it. All of the paragraph names are user-defined. Even if a
program only has one paragraph, it must still have a name. The 'Hello
World' program has a paragraph name MAIN-PARAGRAPH. Regarding
punctuation, as a rule there should only be two full stops in any
paragraph; one after the paragraph name and the other at the end of
the paragraph.

Sub-programs
==========
A program may also refer to a different program, called a sub-program.
A sub-program is an entirely different program from the calling
program, with its own divisions etc... with the exception that it does
not end with STOP RUN (which would return you to the operating
system), but with EXIT PROGRAM. The sub-program is a module, rather
than a subroutine which is what a paragraph could be described as. The
verb CALL is used to activate the sub-program:


000800 DATA DIVISION.
000810 WORKING-STORAGE SECTION.
000820 01 W-DATE-IN PIC 9(6).
000850 LINKAGE SECTION.
000860 01 L-DATE-IN.
000870 03 DAY PIC 99.
000880 03 MONTH PIC 99.
000890 03 YEAR PIC 99.
000900 PROCEDURE DIVISION.
000910 CONTROL-PARAGRAPH.
000920 PERFORM READ-FILE
000930 CALL "VALIDATE-DATE" USING L-DATE-IN
001950 STOP RUN.
003000 IDENTIFICATION DIVISION.
003010 PROGRAM-ID. VALIDATE-DATE.
003020 ...........
............etc.....
003500 PRODECURE DIVISION USING L-DATE-IN.
004000 EXIT PROGRAM.


In the above code, a sub-program is called, named VALIDATE-DATE

In order to use data from the calling program in the sub-program the
calling program uses a section in the data division called the LINKAGE
SECTION. The item W-DATE-IN in the calling program occupies the same
memory address as the sub-program's item L-DATE-IN, so the number
placed in W-DATE-IN item using the VALUE clause is also in L-DATE-IN.
Note: you cannot use VALUE in the linkage section.

The procedure division of the sub-program requiring the use of linkage
section defined data must say so by: PROCEDURE DIVISION USING
...[linkage section items to be used] also refered to by the CALL ...
USING. See lines 000930 and 3500 above.

In the above example, what is being called ("VALIDATE-DATE") is a
literal. This means that you could use an identifier instead, allowing
you a choice between sub-programs depending on what the literal had
been previously defined as. For example, if a record was of type "A"
then you may want to process that record using sub-program
PROCESS-A-REC, but if a type "B" record the use PROCESS-B-REC.
The logic might be as follows:

0003000 IF RECORD-TYPE = "A" THEN
0003010 MOVE "PROCESS-A-REC" TO SUB-PROG
0003020 ELSE MOVE "PROCESS-B-REC" TO SUB-PROG
0003030 CALL SUB-PROG USING L-REC-DATA


Although I haven't described the various commands of the procedure
division the above code is fairly clear...if a marker called
RECORD-TYPE has been set as "A" then place (i.e. MOVE) the string
"PROCESS-A-REC" into the area of memory labelled as SUB-PROG (so now
SUB-PROG contains this string). Otherwise (i.e. ELSE) it is assumed
that the only other type there is can be "B" type and so
"PROCESS-B-REC" is MOVEd into SUB-PROG. Depending on what the item
SUB-PROG contains the desired sub-program will be called.

No comments:

Post a Comment