Microsoft KB Archive/50882

How to Pass Parameters with CHAIN and CALL in COBOL 3.00

PSS ID Number: Q50882 Article last modified on 03-15-1990

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: The information below explains how to CHAIN or CALL another program using Microsoft COBOL Versions 3.00 and 3.00a for MS-DOS and OS/2. The important difference between CALLing and CHAINing to another program is that a CALLed program returns to the CALLing program, whereas a CHAINed program does not return to the CHAINing program. The following information applies to Microsoft COBOL Versions 3.00 and 3.00a.

More Information:

CALLing
A subprogram (also known as a subordinate program, or a CALLed program) is defined as a program that is the object of a CALL statement. The COBOL syntax for a subprogram does not differ from main level code. A COBOL subprogram is defined functionally and doesn’t have a special syntax to distinguish it from a main program. The CALL statement is used to suspend a program’s execution in order to execute another program before returning to the line following the CALL statement. Often, you may want to pass a parameter list to the program being CALLed. In Microsoft COBOL Versions 3.00 and 3.00a, this is accomplished by the following steps: 1. Define the variables to pass in the CALLing program’s WORKING-STORAGE SECTION. 2. In the CALLing program, initialize the variables to be passed. 3. CALL the subordinate program with the following syntax CALL “ subordinate-program” USING where is the directory path to the subordinate program. ( can be omitted if the program is in the same directory). is the list of variables to pass (send). 4. a. Define the variables being passed in the subordinate (CALLed) program in the WORKING-STORAGE SECTION if you want to pass by value (which will not return changes in the parameters to the CALLing program), or b. Define the variables being passed in the CALLed program in the LINKAGE SECTION if you want to pass by reference (which will return changes in the parameters to the CALLing program). 5. List the parameters being received with the following syntax in the subordinate (CALLed) program PROCEDURE DIVISION USING where is the list of variables being received in the exact order they were passed from the CALLing procedure.

CHAINing
The CHAIN statement stops execution of one main program and transfers control to another main program. It does not return unless the CHAINed program explicitly CHAINs back. CHAINing back and forth requires care to ensure proper memory management and to conditionally stop at some point. To pass parameters through a CHAIN, you must add a USING clause to the CHAIN statement. A CHAINed program (a program that is the object of the CHAIN statement) uses a PROCEDURE DIVISION USING statement to receive parameters. In COBOL Versions 3.00 and 3.00a, a PROCEDURE DIVISION USING statement is required in a CHAINing program only if at some time that program itself will be CHAINed or CALLed. In other words, the USING clause of the PROCEDURE DIVISION statement only receives parameters and never sends them. To send parameters, you should use the USING clause of the CALL or CHAIN statement. The code examples below illustrate CALLing and CHAINing.

Code Example 1 – The Main Program: COB1.COB
$SET ANS85 WORKING-STORAGE SECTION. 01 student-rec. 05 student-name pic x(20) value &quot;I is a student&quot;. 05 score       pic 999   value 100. * INDIVIDUAL VARIABLES 77 choice  pic x VALUE SPACES. PROCEDURE DIVISION. 000-main. PERFORM UNTIL choice = '1' OR choice = '2' display &quot;Make selection:&quot; display &quot;Chain to a program   1&quot; display &quot;Call to a program    2&quot; accept choice if choice = '1' chain &quot;cob2&quot; using student-rec else if choice = '2' call &quot;cob2&quot; using student-rec else display &quot;You need to enter 1 or 2&quot; END-IF END-PERFORM. display 'Calling a program returns'. display 'Chaining does not return'. stop run.

Code Example 2 – The CALLed (Subordinate) or CHAINed Program: COB2.COB
$SET ANS85 WORKING-STORAGE SECTION. 01 student-rec. 05 student-name  pic x(20). 05 score         pic 999. PROCEDURE DIVISION using student-rec. 000-main. DISPLAY 'Student Name: ' student-name. DISPLAY 'Score      : ' score. Copyright Microsoft Corporation 1990.