Monday, January 8, 2024

Difference between EXIT PROGRAM, GOBACK, STOP RUN in COBOL

Let us assume we have a Batch JOB step which executes MAIN-PGM. 
Let us assume job step has below call chain. 

MAIN-PGM --> SUB-PGM1 --> SUB-PGM2 --> SUB-PGM3

That is MAIN-PGM calls SUB-PGM1
SUB-PGM1 program inturn calls SUB-PGM2
SUB-PGM2 program inturn calls SUB-PGM3

A run unit is a running set of one or more programs that communicate with each other by COBOL static or dynamic CALL statements. So, the above entire flow is called "run unit"

EXIT PROGRAM or GOBACK in sub programs(called programs) returns the control to the calling program.

For example, if we code EXIT PROGRAM or GOBACK in SUB-PGM3, then control returns to the point in the SUB-PGM2 immediately following the CALL statement that invoked SUB-PGM3

GOBACK in main program MAIN-PGM passes the control to the operating system program that invoked it.

EXIT PROGRAM in main program does NOT do anything, control goes to the next statement that immediately follows EXIT PROGRAM.
So, EXIT PROGRAM works only in sub programs (called programs).

If we code STOP RUN in SUB-PGM3 (or any other program in the calling chain), it will directly pass the control to the operating system program that invoked main program MAIN-PGM. That is STOP RUN ends the execution of entire "run unit".

Friday, January 5, 2024

Initialize FILLER data item

INITIALIZE statement wont initialize FILLER data items. If you want to initialize FILLER items as well, then include "WITH FILLER" in the INITIALIZE statement.

If you include "ALL VALUE" clause in the INITIALIZE statement, then it will initialize ONLY the data items that are defined with VALUE clause to the value specified in the VALUE clause.

Below is the sample code to explain the usage of FILLER and VALUE clauses in the INITIALIZE statement.

WORKING-STORAGE SECTION.
01 WS-X.
   05 WS-A    PIC X(16).
   05 WS-A1   PIC X(16) VALUE '1234567890A.CDEF'.
   05 WS-B    PIC X(5).
   05 WS-C    PIC 9(5).
   05 FILLER  PIC 9(5).
PROCEDURE DIVISION.
   MOVE ALL '*'    TO WS-X
   DISPLAY WS-X
   INITIALIZE WS-X WITH FILLER
   DISPLAY WS-X
   MOVE ALL '*'    TO WS-X
   INITIALIZE WS-X ALL VALUE
   DISPLAY WS-X
   STOP RUN.

Output of the above program

***********************************************    
                                     0000000000    
****************1234567890A.CDEF***************