Call Using Parameters Example


Scenario - Dynamic Call passing parameters from MAINPROG to SUBPROG.

MAINPROG -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. MAINPRWP.
       AUTHOR. MTH.
  
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-INP1  PIC 9(02) VALUE 10.
          05 WS-INP2  PIC 9(02) VALUE 20.
          05 WS-PROG  PIC X(08) VALUE "SUBPRWP".

       PROCEDURE DIVISION.

           DISPLAY "INPUTS (MAIN PROG):  " WS-INP1 ", " WS-INP2.
           CALL WS-PROG USING WS-INP1, WS-INP2.

           STOP RUN.

SUBPROG -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SUBPRWP.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-RESULT    PIC 9(03) VALUE ZEROES.
       LINKAGE SECTION.
       01 LS-INP1      PIC 9(02).
       01 LS-INP2      PIC 9(02).

       PROCEDURE DIVISION USING LS-INP1, LS-INP2.

           COMPUTE WS-RESULT = LS-INP1 * LS-INP2.  
           DISPLAY "RESULT (SUBPROG):  " WS-RESULT.

           GOBACK.

JCL -

//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID     
//***********************************************
//*  RUN A COBOL PROGRAM 
//***********************************************
//STEP01  EXEC PGM=MAINPRWP                      
//STEPLIB  DD  DSN=MATEPK.COBOL.LOADLIB,DISP=SHR
//SYSOUT   DD  SYSOUT=*

Output -

INPUTS (MAIN PROG):  10, 20
RESULT (SUBPROG):  200

Explaining Example -

In the above example:

  • MAINPRWP is the main program, and SUBPRWP is the subprogram.
  • CALL WS-PROG makes the call as dynamic call.
  • WS-INP1 and WS-INP2 are the inputs passed from MAINPRWP to the SUBPRWP.
  • SUBPRWP receives the data into LS-INP1 and LS-INP2 from MAINPRWP, multiplies those values, places the result into WS-RESULT and displays it.