In this topic, we described about the EXIT PROGRAM Statement with detailed example.
The EXIT PROGRAM statement specifies the end of currently executing program and returns control to the calling program. EXIT PROGRAM can specify only in the procedure division of a program. EXIT PROGRAM must not be used in a declarative procedure in which the GLOBAL phrase is specified.
If control reaches an EXIT PROGRAM statement in a program that is called using the CALL statement (that is, the CALL statement is active), control returns to the next statement that immediately follows the CALL statement in the calling routine (program or method).
The state of calling routine is idle until the control returned from the called routine. The EXIT PROGRAM statement execution in called program that has the INITIAL attribute is equivalent to executing a CANCEL statement referencing the called program.
If control reaches an EXIT PROGRAM statement and no CALL statement is active, control passes through the next executable statement. The EXIT PROGRAM statement should be the last statement in a sequence of imperative statements.
When EXIT PROGRAM statement is not last statement, statements following the EXIT PROGRAM will not be executed if a CALL statement is active. When there is no next executable statement in a called program, an implicit EXIT PROGRAM statement (EXIT PROGRAM statement is not coded explicitly) is executed.
Practicle Example -
Main Program - Code:
IDENTIFICATION DIVISION. PROGRAM-ID. MAINPROG. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 INPUT1 PIC 9(02). 01 INPUT2 PIC 9(02). 01 OUT-PUT PIC 9(03). PROCEDURE DIVISION. ACCEPT INPUT1. ACCEPT INPUT2. CALL 'SUBPROG' USING INPUT1, INPUT2, OUT-PUT. DISPLAY OUT-PUT. STOP RUN.
Sub Program - Code:
IDENTIFICATION DIVISION. PROGRAM-ID. SUBPROG. ENVIRONMENT DIVISION. DATA DIVISION. LINKAGE SECTION. 01 IP1 PIC 9(02). 01 IP2 PIC 9(02). 01 OP PIC 9(03). PROCEDURE DIVISION USING IP1,IP2,OP. COMPUTE OP = IP1 * IP2. EXIT PROGRAM.