CALL Using by Value Example


Scenario - Dynamic Call passing parameters using call by content from MAINPROG to SUBPROG.

MAINPROG -

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

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-A        PIC 9(02) VALUE 10.
          05 WS-B        PIC 9(02) VALUE 20.
          05 WS-RESULT   PIC 9(03) VALUE ZEROES.
          05 WS-PROG     PIC X(08) VALUE "SUBPRCC".

       PROCEDURE DIVISION.
           DISPLAY "INPUTS  : " WS-A " " WS-B.
           CALL WS-PROG  USING BY CONTENT
                         WS-A, WS-B RETURNING WS-RESULT.
           DISPLAY "INPUTS AFTER CALL: " WS-A " " WS-B.
           DISPLAY "RESULT IS:  " WS-RESULT.
           STOP RUN.

SUBPROG -

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

       DATA DIVISION.
       LINKAGE SECTION.
       01 LS-INP1       PIC 9(02).
       01 LS-INP2       PIC 9(02).
       01 LS-RESULT     PIC 9(03) VALUE ZEROES.
 
       PROCEDURE DIVISION USING LS-INP1, LS-INP2
                                RETURNING LS-RESULT.
           COMPUTE LS-RESULT = LS-INP1 + LS-INP2
           COMPUTE LS-INP1 = LS-INP1 + 20.
           GOBACK.

JCL -

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

Output -

INPUTS  : 10 20
INPUTS AFTER CALL: 10 20
RESULT IS:  030

Explaining Example -

In the above example:

  • MAINPRCC is the main program, and SUBPRCC is the subprogram.
  • CALL WS-PROG makes the call as dynamic call.
  • WS-INP1 and WS-INP2 are the inputs passed from MAINPRCC to the SUBPRCC using BY CONTENT.
  • SUBPRCC receives the data into LS-INP1 and LS-INP2 from MAINPRCC, adds those values, places the result into WS-RESULT.
  • SUBPRCC adds 20 to the LS-INP1 and returns the control to the main program.
  • LS-INP1 value changes are local to the SUBPRCC and does not affect the value in MAINPRCC.
Note! CALL..BY CONTENT, passes the copy of the variable data to a sub-program, and any value changes made by the sub-program are not reflected in the main program.