Microsoft KB Archive/50462

COBOL 2.x/3.00/3.00a Don’t Support Recursive Subprogram Calls

PSS ID Number: Q50462 Article last modified on 11-13-1989

2.00 2.10 2.20 3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: Microsoft COBOL Versions 2.00, 2.10, 2.20, 3.00, and 3.00a do not support recursion of subprogram calls. In COBOL 3.00 and 3.00a, a recursive CALL produces run-time error 166, “Recursive COBOL CALL is illegal,” and the program terminates. In COBOL 2.x, the CALL is successful, but because it is not true recursion, it eventually gives the following error: ** Run-Time Error: Insufficient memory for program, Program:, line: #####. This error is generated because the subprogram is reloaded into memory multiple times and eventually uses up all memory. This information applies to Microsoft COBOL Compiler Versions 2.00, 2.10, and 2.20 for MS-DOS and to Microsoft COBOL Compiler Versions 3.00 and 3.00a for MS-DOS and MS OS/2.

More Information: Recursion is the act of a subprogram or function calling itself. Recursion causes the same subprogram to begin execution again before the previous call to it has finished. It executes itself and then returns and continues execution of the subprogram exactly where it left off before the recursive call was made. To support recursion, the language must allocate local variables for subprograms on the program stack. In COBOL 2.x, 3.00, and 3.00a, subprogram variables are not allocated on the stack but have permanent addresses until the whole subprogram is removed from memory with a CANCEL. This means that a recursive COBOL subprogram would destroy its own local variables by permanently altering them during the recursion. To support recursion, a language must be able to execute the code of the same procedure or function again before the current execution of that procedure or function is finished without causing any side effects. This is why COBOL 2.x does not support recursion. When a subprogram calls itself in COBOL 2.x, instead of executing the same code over again, another copy of the subprogram is loaded into memory, and that code is executed. When enough copies of the subprogram are loaded, an “Insufficient memory for program” error is generated. Another limitation of COBOL that makes recursion more difficult to use is that COBOL 2.x, 3.00, and 3.00a cannot pass parameters by value. Many of the uses for recursion require passing parameters by value. COBOL 2.x, 3.00, and 3.00a support passing parameters only by reference.

Code Example
The following code example can be used to show that attempting a recursive CALL in COBOL produces a run-time error. This code can be used in COBOL 2.x, 3.00, and 3.00a. The following is the main program that CALLs the recursive procedure: IDENTIFICATION DIVISION. PROGRAM-ID. MAIN. DATA DIVISION. WORKING-STORAGE SECTION. 77 FLAG PIC 9(4) VALUE 0. PROCEDURE DIVISION. 000-MAIN. DISPLAY “INSIDE OF THE MAIN PROGRAM”. ADD 1 TO FLAG. CALL “RECURSE” USING FLAG. DISPLAY “INSIDE OF THE MAIN PROGRAM”. STOP RUN. The following is the recursive subprogram: IDENTIFICATION DIVISION. PROGRAM-ID. RECURSE. DATA DIVISION. WORKING-STORAGE SECTION. 77 FLAG PIC 9(4) VALUE 0. LINKAGE SECTION. 01 FLAG2 PIC 9(4). PROCEDURE DIVISION USING FLAG2. 000-MAIN. EXHIBIT FLAG. EXHIBIT “INSIDE OF THE RECURSE PROGRAM”. ADD 1 TO FLAG. EXHIBIT FLAG. IF FLAG2 NOT = 2 CALL “RECURSE” USING FLAG. EXHIBIT “BACK FROM RECURSION”. EXHIBIT FLAG. EXIT PROGRAM.

Copyright Microsoft Corporation 1989.