Cancel Statement Example


Scenario - CANCEL statement usage in main program.

MAINPROG -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION. 
       PROGRAM-ID. MAINPRWR.
       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-RESULT      PIC 9(03) VALUE ZEROES.
          05 WS-PROG        PIC X(08) VALUE "SUBPROG".

       PROCEDURE DIVISION.
	  * Calling subprogram with two inputs and receiving the result 
           CALL WS-CALLING-PROG USING WS-INP1, WS-INP2, WS-RESULT. 

           DISPLAY "INPUTS:  " WS-INP1 ", " WS-INP2.
           DISPLAY "RESULTS: " WS-RESULT.

		   CANCEL WS-CALLING-PROG.
		   ...
           STOP RUN.

SUBPROG -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. SUBPRWR.
       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, LS-RESULT.

           COMPUTE LS-RESULT = LS-INP1 + LS-INP2.
           GOBACK.

Explaining Example -

In the above example:

  • MAINPRWR is the main program and SUBPRWR is the subprogram. "CANCEL WS-CALLING-PROG" releases all the resources used by SUBPRWR.