The PROCEDURE Division

3.4 The PROCEDURE DIVISION

The procedure division is where the logic of the program actually found. Here is where the various commands are written (see Commands and logic section).

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 (see Commands and logic sections) 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.