COBOL COBOL 2014 (ISO/IEC 1989:2014), built with GnuCOBOL 3.x ยท
โ verified by execution on 2026-08-16
When a COBOL program finishes executing, it rarely does so in isolation. In mainframe environments, your program is usually just one step in a much larger Job Control Language (JCL) job schedule. The operating system needs to know if your program completed successfully or if it encountered an error, so it can decide whether to run the subsequent steps or halt the entire job.
This communication bridge between your COBOL program and the operating system is built using the RETURN-CODE special register and the JCL COND parameter.
The RETURN-CODE Special Register
The RETURN-CODE is a predefined special register implicitly defined by the COBOL compiler. You do not need to declare it in your DATA DIVISION. In GnuCOBOL, it is implicitly defined as a PIC S9(9) USAGE BINARY field initialized to zero when the program starts.
By standard convention across the industry:
0: Success
4: Warning (minor issues, but execution can continue)
8: Error (significant issues, subsequent steps usually skipped)
12: Severe Error (fatal issues, job halts)
16: Terminal Error (catastrophic failure)
To pass a status back to the operating system, you simply MOVE a numeric value into this register before your program ends.
cobolโ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. SET-RC. PROCEDURE DIVISION. DISPLAY 'Processing data...' *> An error occurred, so we set a warning code: MOVE 4 TO RETURN-CODE DISPLAY 'Program exiting with RC: ' RETURN-CODE *> Reset to 0 for the automated test runner: MOVE 0 TO RETURN-CODE STOP RUN.
Output
Processing data...
Program exiting with RC: +000000004
Your output
Note: The automated execution environment on this page requires programs to exit with 0 to mark a successful test run, which is why we reset it to 0 right before STOP RUN. In a real production program, you would leave it at 4.
JCL and the COND Parameter
Once your program exits, the operating system (e.g., z/OS) looks at the return code. If you have subsequent job steps lined up in your JCL, you can use the COND (Condition) parameter to tell the OS whether to execute them or bypass them.
Here is a visual representation of how job flow works:
The Counter-Intuitive Nature of COND
The most critical thing to understand about the JCL COND parameter is that it defines the condition for BYPASSING the step, not running it.
When you write COND=(4,EQ) in JCL, it translates to: โIf 4 is Equal to the return code of the previous step, SKIP this step.โ
If the statement evaluates to TRUE, the step is bypassed. If it is FALSE, the step runs.
Dynamic Return Codes
You can use standard COBOL logic to determine the return code based on runtime conditions.
cobolโ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. CALC-RC. DATA DIVISION. WORKING-STORAGE SECTION. 01 RECORD-COUNT PIC 9(3) VALUE 500. PROCEDURE DIVISION. IF RECORD-COUNT > 200 DISPLAY 'Warning: Unusually high record count.' MOVE 4 TO RETURN-CODE ELSE MOVE 0 TO RETURN-CODE END-IF DISPLAY 'Final RC will be ' RETURN-CODE *> Reset for test runner MOVE 0 TO RETURN-CODE STOP RUN.
Output
Warning: Unusually high record count.
Final RC will be +000000004
Your output
Check Your Understanding
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
What does the JCL parameter `COND=(8,LT)` mean for the current step?
Reveal answer
Bypass this step if 8 is less than the previous return code. โ COND logic is evaluated as: `IF (number) (operator) (return code) THEN BYPASS`. So `8 < RC` means 'if 8 is less than the RC, skip it.'
Where must `RETURN-CODE` be defined in a COBOL program?
Reveal answer
Nowhere, it is a special register implicitly defined by the compiler. โ `RETURN-CODE` is a built-in special register and requires no manual declaration in the DATA DIVISION.
What happens if you never set `RETURN-CODE` in your program?
Reveal answer
It defaults to 0 and passes 0 to the operating system. โ The special register is initialized to zero when the program starts. If untouched, it exits with zero (success).
Challenges
Challenge 1 +20 XP
Write a program that takes an input number. If the number is even, set RETURN-CODE to 0. If odd, set RETURN-CODE to 4.
cobolโ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real โ it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 (input: "4") โ expects "Done"
Show solution (0 XP)
IDENTIFICATION DIVISION.PROGRAM-ID. CHECK-EVEN.DATA DIVISION.WORKING-STORAGE SECTION.01 INPUT-NUM PIC 9.01 WS-REMAINDER PIC 9.PROCEDURE DIVISION. ACCEPT INPUT-NUM. COMPUTE WS-REMAINDER = FUNCTION MOD(INPUT-NUM, 2). IF WS-REMAINDER = 0 MOVE 0 TO RETURN-CODE ELSE MOVE 4 TO RETURN-CODE END-IF. DISPLAY 'Done'. MOVE 0 TO RETURN-CODE STOP RUN.
๐ Bug Hunt+50 XP
Bug hunt: A programmer thought they needed to declare RETURN-CODE before using it, but now the program won't compile! The compiler says ''RETURN-CODE' is ambiguous'. Fix the code by removing the unnecessary definition.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it โ the tests below decide when you are right.
cobolโ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real โ it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 โ expects "Done\n"
Need a hint? (โ25% XP)
RETURN-CODE is a special register. You don't need to define it in WORKING-STORAGE.
Show solution (0 XP)
IDENTIFICATION DIVISION.PROGRAM-ID. ACCUMULATOR.DATA DIVISION.WORKING-STORAGE SECTION.01 TOTAL PIC 9(3) VALUE 150.PROCEDURE DIVISION. IF TOTAL > 100 MOVE 8 TO RETURN-CODE ELSE MOVE 0 TO RETURN-CODE END-IF. DISPLAY 'Done'. MOVE 0 TO RETURN-CODE STOP RUN.