Summary -

In this topic, we described about the below sections -

The PROCEDURE DIVISION consists of optional declaratives and procedures that contain sections, paragraphs, sentences and statements. The PROCEDURE DIVISION contains the actual logic or operations to be performed to complete the task.

The PROCEDURE DIVISION may contain declarative as well as non-declarative procedures. Program execution starts from PROCEDURE DIVISION. PROCEDURE DIVISION ends with STOP RUN or GO BACK statements.

Syntax -

PROCEDURE DIVISION Syntax

1) Normal Main program procedure division

PROCEDURE DIVISION

2) Sub program procedure division

PROCEDURE DIVISION [USING {data-name-1}...]

3) Procedure division with declaratives.

PROCEDURE DIVISION.
 DECLARATIVES.
 -----------
 -----------
 END DECLARATIVES.

USING Clause -

Specifies the parameters that a program receives when the program is called or invoked.

The USING phrase is valid in the procedure division header of a called subprogram. Each USING identifier must be defined as a level-01 or level-77 item in the linkage section of the called subprogram.

A data item specified in the USING phrase of the CALL statement can be a data item of any level in the data division of the calling COBOL program. Each USING phrase data item of an INVOKE statement can be defined of any level in the data division of the calling COBOL program.

A data item in the USING phrase of the procedure division header can have a REDEFINES clause. It is possible to call COBOL programs from non-COBOL programs or to pass user parameters from a system command to a COBOL main program.

The appearing order of USING identifiers in both calling and called programs should be same correspondence to transfer the data. The correspondence is positional and not by name. For calling and called subprograms, corresponding identifiers must contain the same number of bytes although their data definitions need not be the same.

For index-names, no correspondence is established. Index-names in calling and called programs, always refer to separate indexes.

The identifiers specified in a CALL USING statement name the data items available to the calling program that can be referred to in the called program. These items can be defined in any data division section.

If an identifier appears more than once in a procedure division USING phrase, the last value passed to it by a CALL statement is used.


Practical Example -

Scenario - Below example describes how the LINKAGE section is used to pass the data between the programs.

Main Program -

Main program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. MAINPROG.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       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.                                                    
**************************** Bottom of Data ****************************

Sub Program -

sub program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. MAINPROG.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       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.                                                    
**************************** Bottom of Data ****************************

Jcl -

Procedure Division Example Run Jcl

Output -

Procedure Division Example Output

Explaining Example -

In the above example, MAINPROG is the main program and SUBPROG is the subprogram. Note that USING clause with PROCEDURE DIVISION can use in subprogram only.

WS-IP1, WS-IP2 are the inputs passed from MAINPROG to the SUBPROG. SUBPROG receives the data into LN-IP1, LN-IP2 from MAINPROG, multiply those values and place the result into LN-RESULT.

SUBPROG returns the output LN-RESULT to MAINPROG, MAINPROG displays the result received from SUBPROG.


RETURNING phrase -

The RETURNING phrase specifies a data item that is to receive the program result. Do not use the procedure division RETURNING phrase in -

  • Programs that contain the ENTRY statement.
  • Nested programs.
  • Main programs.

data-name-2

data-name-2 is the RETURNING data item. data-name-2 must be a level-01 or level-77 item in the linkage section. The RETURNING data item is an output-only parameter.


Practical Example -

Scenario - Below example describes how the LINKAGE section is used to pass the data between the programs.

Main Program -

Main program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. PRMAINPR.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       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 "PRSUBPR".                 
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           ACCEPT WS-IP1.                                               
           ACCEPT WS-IP2.                                               
           CALL WS-CALLING-PROG                                         
                USING BY REFERENCE                                      
                WS-IP1, WS-IP2                                          
                RETURNING WS-RESULT.                                    
                                                                        
           DISPLAY "INPUTS:  " WS-IP1 ", " WS-IP2.                      
           DISPLAY "RESULTS: " WS-RESULT.                               
                                                                        
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Sub Program -

sub program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. PRSUBPR.                                             
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       LINKAGE SECTION.                                                 
       01 LN-IP1             PIC 9(02).                                 
       01 LN-IP2             PIC 9(02).                                 
       01 LN-RESULT          PIC 9(04).                                 
                                                                        
       PROCEDURE DIVISION USING                                         
                 BY REFERENCE LN-IP1, LN-IP2 RETURNING LN-RESULT.       
                                                                        
           COMPUTE LN-RESULT = LN-IP1 * LN-IP2.                         
           GOBACK.                                                      
**************************** Bottom of Data ****************************

Jcl -

Procedure Division Example Run Jcl

Output -

Procedure Division Example Output

Explaining Example -

In the above example, PRMAINPR is the main program and PRSUBPR is the subprogram. Note that USING clause can use in subprogram only.

WS-IP1, WS-IP2 are the inputs passed from PRMAINPR to the PRSUBPR USING CALL BY REFERNCE and RETURING clause. PRSUBPR receives the data into LN-IP1, LN-IP2 from PRMAINPR, multiply those values and place the result into LN-RESULT.

PRSUBPR returns the output LN-RESULT to PRMAINPR, PRMAINPR displays the result received from PRSUBPR.


Procedure division with declaratives -

Syntax -

PROCEDURE DIVISION.
 DECLARATIVES.
 -----------
 -----------
 END DECLARATIVES.

When Declaring the DECLARATIVES in the PROCEDURE DIVISION, it must be the first section immediately after PROCEDURE DIVISION and should ending with the END DECLARATIVES.

A declarative sentence contains a USE statement. The USE statements itself not executed and calling conditions of USE procedures.

These declarative procedures are optional paragraphs. END DECLARATIVES indicate the end of the list of declarative sections.

Note! The PROCEDURE DIVISION begins in Area A and all its statements begins in Area B.

Practical Example -

Scenario - Below example describes how the declaratives use in the COBOL program.

Program -

Declaratives program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. PRDECLAR.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       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.                                                        
**************************** Bottom of Data ****************************

Compile Jcl -

Declaratives Example Compile Jcl

Explaining Example -

To enable the program debugging, the program should compile with TEST (TEST(HOOK,NOSEPARATE,NOEJPD)) option. Once the program is compiled successfully, program trace stat displaying in the output.