COBOL EXIT PROGRAM Statement

EXIT PROGRAM indicates the end of the subprogram (called program) processing and returns control to the main program (calling program). It is mainly used in COBOL subprograms.

Syntax -

EXIT PROGRAM.

Points to Note -

  • EXIT PROGRAM statement should be the last in a sequence of statements.
  • If no CALL is active, it means the program is not called from any program, and the EXIT PROGRAM is executed, the control returns to the OS.
  • If the CALL is active and the EXIT PROGRAM is executed, the control returns to the calling program and the following statements after CALL after CALL starts executing.

Practical Example -

Scenario - Static Call from MAINPROG to SUBPROG and receiving the result back from SUBPROG. EXIT PROGRAM usage in SUBPROG.

MAINPROG -

----+----1----+----2----+----3----+----4----+----5----+
       ...
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-INP1      PIC 9(02) VALUE 47. *> Input1
          05 WS-INP2      PIC 9(02) VALUE 25. *> Input2
          05 WS-RESULT    PIC 9(04).          *> Result Variable
       ...
	   PROCEDURE DIVISION. 
	  * Calling subprogram staically with two inputs 
      * and receiving the result from SUBPROG
           CALL "SUBPROG" USING WS-INP1, WS-INP2, WS-RESULT.
		   
           DISPLAY "INPUTS:  " WS-INP1 ", " WS-INP2.
           DISPLAY "RESULTS: " WS-RESULT.
		   STOP RUN

SUBPROG -

----+----1----+----2----+----3----+----4----+----5----+
       ...
       DATA DIVISION.
       LINKAGE SECTION.
       01 LK-INP1      PIC 9(02).  *> To receive input1 from MAINPROG
       01 LK-INP2      PIC 9(02).  *> To receive input2 from MAINPROG
       01 LK-RESULT    PIC 9(04).  *> To send result to MAINPROG
       ...
	  *Receiving data from main program CALL statement
       PROCEDURE DIVISION USING LK-INP1, LK-INP2, LK-RESULT. 
	   
           COMPUTE LK-RESULT = LK-INP1 * LK-INP2.
           EXIT PROGRAM.

JCL -

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

Output -

INPUTS:  47, 25
RESULTS:  1175