PROCEDURE DIVISION
PROCEDURE DIVISION
- PROCEDURE DIVISION is one of the four main divisions of a COBOL program.
- It is where the program's logic is written and is responsible for carrying out the data processing tasks coded in the program.
- It consists of sections, paragraphs, sentences, and statements that are the logic used to complete the task.
- Program execution starts from PROCEDURE DIVISION and ends with STOP RUN, GO BACK, or EXIT program statements.
Syntax -
Main program -
PROCEDURE DIVISION.
[section-name SECTION.]
paragraph-name.
[statements...]
Sub program -
PROCEDURE DIVISION [USING {variable-1, variable-2, ...}
RETURNING {variable-a, variable-b, ...}].
[section-name SECTION.]
paragraph-name.
[statements...]
With declaratives -
PROCEDURE DIVISION.
DECLARATIVES.
-----------
-----------
END DECLARATIVES.
Note!
- All statements coded in [ ] are optional.
- PROCEDURE DIVISION begins in Area A and all its statements begins in Area B.
Parameters -
- USING variable-1, variable-2, ... - Optional. It is used to send the data from main program or receive the data in sub program. variable-1, variable-2, ... should be declared in the LINKAGE SECTION of the subprogram.
- RETURNING variable-a, variable-b, ... - Optional. It is coded in a subprogram and used to send the result to the main program.
- section-name SECTION - Optional. It is a section declaration, and a section is named and followed by the keyword SECTION.
- paragraph-name - Represents a name for the COBOL statements block. Paragraphs are mainly used to organize the logic in the PROCEDURE DIVISION.
- statements - Formed by COBOL keywords to perform an operation.
Practical Example -
Scenario - PROCEDURE DIVISION usage in both main program and subprogram.
MAINPROG -
----+----1----+----2----+----3----+----4----+----5----+
...
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-IP1 PIC 9(02).
05 WS-IP2 PIC 9(02).
05 WS-RESULT PIC 9(04).
05 WS-CALLING-PROG PIC X(08) VALUE "SUBPROG".
...
PROCEDURE DIVISION.
ACCEPT WS-IP1.
ACCEPT WS-IP2.
CALL WS-CALLING-PROG USING WS-IP1, WS-IP2, WS-RESULT.
DISPLAY "INPUTS: " WS-IP1 ", " WS-IP2.
DISPLAY "RESULTS: " WS-RESULT.
STOP RUN.
SUBPROG -
----+----1----+----2----+----3----+----4----+----5----+
...
LINKAGE SECTION.
01 LN-IP1 PIC 9(02).
01 LN-IP2 PIC 9(02).
01 LN-RESULT PIC 9(04).
...
PROCEDURE DIVISION USING
LN-IP1, LN-IP2 RETURNING LN-RESULT.
COMPUTE LN-RESULT = LN-IP1 * LN-IP2.
GOBACK.
JCL -
//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID //* //STEP01 EXEC PGM=MAINPROG //STEPLIB DD DSN=MATEPK.COBOL.LOADLIB,DISP=SHR //SYSOUT DD SYSOUT=* //SYSIN DD * 47 25 /*
Output -
INPUTS: 47, 25 RESULTS: 1175
PROCEDURE DIVISION With declaratives -
Info! PROCEDURE DIVISION with declaratives are not using nowadays.
It has been explained here so that you can better understand it if you encounter it in an existing program.
- Declarative is used to code one or more special-purpose sections executed when an exceptional condition occurs.
- The declarative should be the first section immediately after PROCEDURE DIVISION and end with the END DECLARATIVES.
Syntax -
PROCEDURE DIVISION.
DECLARATIVES.
-----------
-----------
END DECLARATIVES.
Practical Example -
Scenario - Below example describes how the declaratives use in the COBOL program.
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
WORKING-STORAGE SECTION.
01 WS-VARS.
05 WS-VAR1 PIC 9(02) VALUE 22.
05 WS-VAR2 PIC 9(02) VALUE 47.
05 WS-MRESULT PIC 9(04).
05 WS-ARESULT PIC 9(04).
PROCEDURE DIVISION.
DECLARATIVES.
DEBUG-DECLARATIVES SECTION.
USE FOR DEBUGGING ON ALL PROCEDURES.
DEBUG-DECLARATIVES-PARAGRAPH.
DISPLAY "TRACE FOR PROCEDURE-NAME : " DEBUG-NAME.
END DECLARATIVES.
MAIN-PROCESSING.
PERFORM PARA-ADDITION.
PERFORM PARA-MULTIPLY.
STOP RUN.
PARA-ADDITION.
COMPUTE WS-ARESULT = WS-VAR1 + WS-VAR2.
DISPLAY "ADDITION RESULT: " WS-ARESULT.
PARA-ADDITION-EXIT.
EXIT.
PARA-MULTIPLY.
COMPUTE WS-MRESULT = WS-VAR1 * WS-VAR2.
DISPLAY "MULTIPLICATION RESULT: " WS-MRESULT.
PARA-MULTIPLY-EXIT.
EXIT.
Compile JCL -
Explaining Example -
To enable the program debugging, the program should compile with TEST option. Once the program is compiled successfully, program trace stat displaying in the output.