Microsoft KB Archive/51574

Passing Parameters in COBOL 2.x with CHAIN and CALL

PSS ID Number: Q51574 Article last modified on 04-20-1993

2.00 2.10 2.20 MS-DOS

Summary: This article explains how to CHAIN or CALL another program compiled with Microsoft COBOL Compiler Versions 2.0, 2.1, or 2.2. The difference between CALLing and CHAINing is a CALLed program will return to the CALLing program and a CHAINed program will not.

More Information: The CALL statement suspends a program’s execution, invokes another program, and returns back to the line following the CALL statement. The CHAIN statement stops execution of the main program (the program specified in the RUNCOB command) to invoke the subordinate program (or subprogram). The main program is not returned to unless the subordinate program explicitly CHAINs back to it. This type of dual CHAINing requires care to ensure proper memory management and that there is a condition to stop the recursive CHAINing. In both the CALL and CHAIN statements, a variable list can be passed to the subordinate program (see below for an explanation on how this is achieved). In Microsoft COBOL Versions 2.0, 2.1, and 2.2, the CHAIN and CALL statements can pass parameters as follows: 1. Define the variables to be passed in the main program’s WORKING-STORAGE SECTION. 2. Initialize the variables to be passed in the main program. 3. CALL (or CHAIN to) the subordinate program with the following syntax CALL “ -program” USING. CHAIN “ -program” USING. where: is the disk directory path to the subordinate program ( can be omitted if the program is in the same directory). is the list of variables to pass. 4. Define the variables being passed in the subordinate program in the WORKING-STORAGE SECTION (in a CALLed program this must be done in the LINKAGE SECTION) of the DATA DIVISION. 5. List the parameters being passed in the subordinate program with the following syntax PROCEDURE DIVISION CHAINING (for CHAINED programs) PROCEDURE DIVISION USING (for CALLed programs) where: is the list of variables being passed in the exact order they were passed from the main program. 6. The CALLed program must have an EXIT PROGRAM statement to return control back to the main CALLing program. Furthermore, the EXIT PROGRAM statement must be a paragraph itself. If used in a main (CALLing) program, the EXIT PROGRAM statement is ignored. The code examples below illustrate this explanation:

Code Example
Source File 1 – The main program: MAIN22.COB IDENTIFICATION DIVISION. PROGRAM-ID. MAIN22. ENVIRONMENT DIVISION. OBJECT-COMPUTER. IBM. FILE-CONTROL. DATA DIVISION. WORKING-STORAGE SECTION. 01 STUDENT-REC. 05 STUDENT-NAME PIC X(20) VALUE “I is a student”. 05 SCORE1 PIC 999 VALUE 100. * INDIVIDUAL VARIABLES 77 STALL PIC X. 77 CHOICE PIC X. PROCEDURE DIVISION CHAINING STUDENT-REC. 000-MAIN. DISPLAY “Make selection:”. DISPLAY “Chain to a program 1”. DISPLAY “Call to a program 2”. ACCEPT CHOICE. IF CHOICE = ‘1’ CHAIN “CHAIN22” USING STUDENT-REC ELSE IF CHOICE = ‘2’ CALL “CALLED22” USING STUDENT-REC ELSE DISPLAY “You need to enter 1 or a 2”. DISPLAY ‘Calling a program returns’. DISPLAY ‘Chaining does not return’. STOP RUN. '' Source File 2 – The chained program: CHAIN22.COB IDENTIFICATION DIVISION. PROGRAM-ID. CHAIN22. ENVIRONMENT DIVISION. OBJECT-COMPUTER. IBM. FILE-CONTROL. DATA DIVISION. WORKING-STORAGE SECTION. 01 STUDENT-REC. 05 STUDENT-NAME PIC X(20). 05 SCORE1 PIC 999. '' INDIVIDUAL VARIABLES 77 STALL PIC X. PROCEDURE DIVISION CHAINING STUDENT-REC. 000-MAIN. DISPLAY ‘Student Name:’. DISPLAY STUDENT-NAME. DISPLAY ‘Hit Return, I am finished’. ACCEPT STALL. STOP RUN. '' Source File 3 – The called program: CALLED22.COB IDENTIFICATION DIVISION. PROGRAM-ID. CALLED22. ENVIRONMENT DIVISION. OBJECT-COMPUTER. IBM. FILE-CONTROL. DATA DIVISION. WORKING-STORAGE SECTION. LINKAGE SECTION. 01 STUDENT-REC. 05 STUDENT-NAME PIC X(20). 05 SCORE1 PIC 999. '' INDIVIDUAL VARIABLES 77 STALL PIC X. PROCEDURE DIVISION USING STUDENT-REC. 000-MAIN. DISPLAY ‘Student Name:’. DISPLAY STUDENT-NAME. DISPLAY ‘Hit Return, I will return to main’. ACCEPT STALL. PERFORM 100-EXIT. '' 100-EXIT. EXIT PROGRAM. '' Compile the above COBOL programs as follows: COBOL MAIN22.COB; COBOL CHAIN22.COB; COBOL CALLED22.COB; You can run the main COBOL program, MAIN22.INT, as follows: RUNCOB MAIN22 The following is the output from the MAIN22 program given a “Make selection” response of 1: Student Name: I is a student Hit Return, I am finished The following is the output from the MAIN22 program given a “Make selection” response of 2: Student Name: I is a student Hit Return, I will return to main Calling a program returns Chaining does not return

Additional reference words: 2.00 2.10 2.20 Copyright Microsoft Corporation 1993.