In this topic, we described about the GOBACK Statement with detailed example.
GOBACK gives the control back from where it received. The GOBACK statement specifies the logical end of a called program or invoked method.
The GOBACK statement works like the EXIT PROGRAM statement when coded in called program and like the STOP RUN statement when coded in a main program.
If control reaches a GOBACK statement and a CALL statement is active, control returns to the next immediate statement of the CALL statement in the calling program or method.
GOBACK can be coded in both main program and sub program. If GOBACK coded in the Main program, returns the control to the system.
If GOBACK coded in the sub program, returns the control to the calling program. GOBACK is a PROCEDURE DIVISION statement which should be always coded in AREA B.
GOBACK must not be used in a declarative procedure in which the GLOBAL phrase is specified. The state of calling routine is idle until the control returned from the called routine.
The GOBACK statement execution in called program that has the INITIAL attribute is equivalent to executing a CANCEL statement referencing the called program.
A GOBACK statement should appear as only statement or as the last statement of a series of imperative statements. Because any statements after the GOBACK statement are not executed.
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. GO BACK.