Microsoft KB Archive/51111

COBOL 3.0 LINKAGE SECTION Receives and Returns by Reference

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

3.00 3.00a MS-DOS

Summary: This article discusses the LINKAGE SECTION in Microsoft COBOL Compiler Versions 3.0 and 3.0a for MS-DOS and MS OS/2. In COBOL, you can define variables in the FILE or WORKING-STORAGE SECTIONs. The USING clause of the CALL statement lets you pass specified variables as parameters to a subprogram. The LINKAGE SECTION in a CALLed subprogram accepts parameters by reference and returns any changes in the parameter values to the CALLing program. If you don’t use a LINKAGE SECTION in the CALLed program, then variables changed in the CALLed program are not passed back to the CALLing program. Therefore, not using a LINKAGE SECTION is similar to passing by value. Note that a CALLing program doesn’t need to contain a LINKAGE SECTION, but a CALLed program must use a LINKAGE SECTION when you desire to pass by reference.

More Information: Note: Not using a LINKAGE SECTION may make the code nonportable since this is outside of the ANSI standard and is a feature specific to Microsoft COBOL Versions 3.0 and 3.0a. Microsoft recommends using a LINKAGE SECTION, which makes more efficient use of memory. There are two requirements to pass parameters in a CALL in COBOL 3.0 and 3.0a, as follows: 1. There must be a USING key word in both the CALL statement of the CALLing program and in the PROCEDURE DIVISION statement of the CALLed program. If the USING clause does not appear in both places, the parameter values will be corrupted. 2. The sizes and PICTURE clauses of the parameters in the USING statements must match between the CALLing and CALLed programs. If the parameters are not matched, an error message will not be generated, but the values in the variables will not be passed correctly and may appear to be corrupted. To dynamically link the programs below, use the following compile and link instructions: COBOL caller.cob; [ the main module ] COBOL called.cob; [ the CALLed module ] LINK caller+@adis.lnk; [ link the main module ] LINK called; [ link separate for Dynamic link ] To statically link the programs, replace the two previous link lines with the one following link line: LINK caller+called+@adis.lnk;

Code Example 1
The following two programs demonstrate not using a LINKAGE SECTION in the CALLed program, thus passing by value. CALLing program: $SET ANS85 PROGRAM-ID. CALLER. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. DATA DIVISION. WORKING-STORAGE SECTION. 01 SEND-RECORD. 05 STAT-1 PIC 9(3) OCCURS 5 TIMES. 01 NUM-1 PIC 9. SCREEN SECTION. 01 BLANK-SCREEN. 05 BLANK SCREEN. PROCEDURE DIVISION. 100-MAIN. DISPLAY BLANK-SCREEN. DISPLAY ‘ENTER FIVE NUMBERS’. PERFORM 200-GET-DATA VARYING NUM-1 FROM 1 BY 1 UNTIL NUM-1 = 6. DISPLAY ‘LETS SEND’;NUM-1;’ TO THE OTHER PROGRAM’. CALL “COB5A” USING NUM-1 SEND-RECORD. DISPLAY ‘BACK FROM THE CALL’. DISPLAY “VALUES IN THE ARRAY AFTER THE CALL.”. DISPLAY “THESE VALUES SHOULD NOT BE CHANGED BY THE CALL.”. PERFORM 200-DISPLAY-DATA VARYING NUM-1 FROM 1 BY 1 UNTIL NUM-1 = 6. STOP RUN. 200-GET-DATA. ACCEPT STAT-1(NUM-1). 200-DISPLAY-DATA. EXHIBIT STAT-1(NUM-1). CALLed program: $SET ANS85 PROGRAM-ID. COB5A. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. DATA DIVISION. WORKING-STORAGE SECTION. 01 TEST-2 PIC 9. 01 PAUSE PIC X. * THIS IS WHERE THE LINKAGE SECTION WOULD GO 01 TEST-3 PIC 9. 01 SEND-RECORD. 05 TEST-1 PIC 9(3) OCCURS 5 TIMES. SCREEN SECTION. 01 BLANK-SCREEN. 05 BLANK SCREEN. PROCEDURE DIVISION USING TEST-3 SEND-RECORD. DISPLAY BLANK-SCREEN. DISPLAY ’ ‘. DISPLAY ’IN OTHER PROGRAM’. DISPLAY ’ ‘. DISPLAY “CONTENTS OF THE ARRAY”. PERFORM 100-OUT-1 VARYING TEST-2 FROM 1 BY 1 UNTIL TEST-2 = 6. DISPLAY ’THIS IS THE NUMBER SENT’;TEST-3. DISPLAY ‘HIT  TO GO BACK’. ACCEPT PAUSE AT 1040. DISPLAY BLANK-SCREEN. EXIT PROGRAM. 100-OUT-1. EXHIBIT TEST-1(TEST-2). COMPUTE TEST-1(TEST-2) = TEST-2.

Code Example 2
The following two programs demonstrate using a LINKAGE SECTION in the CALLed program, thus passing by reference. CALLing program: $SET ANS85 PROGRAM-ID. CALLER2. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. DATA DIVISION. WORKING-STORAGE SECTION. 01 SEND-RECORD. 05 STAT-1 PIC 9(3) OCCURS 5 TIMES. 01 NUM-1 PIC 9. SCREEN SECTION. 01 BLANK-SCREEN. 05 BLANK SCREEN. PROCEDURE DIVISION. 100-MAIN. DISPLAY BLANK-SCREEN. DISPLAY ‘ENTER FIVE NUMBERS’. PERFORM 200-GET-DATA VARYING NUM-1 FROM 1 BY 1 UNTIL NUM-1 = 6. DISPLAY ‘LETS SEND’;NUM-1;’ TO THE OTHER PROGRAM’. CALL “COB5B” USING NUM-1 SEND-RECORD. DISPLAY ‘BACK FROM THE OTHER ONE DUDE’. DISPLAY “VALUE IN THE ARRAY AFTER CALL.”. DISPLAY “THESE VALUES SHOULD BE CHANGED BY THE CALL.”. PERFORM 200-DISPLAY-DATA VARYING NUM-1 FROM 1 BY 1 UNTIL NUM-1 = 6. STOP RUN. 200-GET-DATA. ACCEPT STAT-1(NUM-1). 200-DISPLAY-DATA. EXHIBIT STAT-1(NUM-1). CALLed program: $SET ANS85 PROGRAM-ID. COB5B. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. DATA DIVISION. WORKING-STORAGE SECTION. 01 TEST-2 PIC 9. 01 PAUSE PIC X. LINKAGE SECTION. 01 TEST-3 PIC 9. 01 SEND-RECORD. 05 TEST-1 PIC 9(3) OCCURS 5 TIMES. SCREEN SECTION. 01 BLANK-SCREEN. 05 BLANK SCREEN. PROCEDURE DIVISION USING TEST-3 SEND-RECORD. DISPLAY BLANK-SCREEN. DISPLAY ’ ‘. DISPLAY ’IN OTHER PROGRAM’. DISPLAY ’ ‘. DISPLAY “CONTENTS OF THE ARRAY”. PERFORM 100-OUT-1 VARYING TEST-2 FROM 1 BY 1 UNTIL TEST-2 = 6. DISPLAY ’THIS IS THE NUMBER SENT’;TEST-3. DISPLAY ‘HIT  TO GO BACK’. ACCEPT PAUSE AT 1040. DISPLAY BLANK-SCREEN. EXIT PROGRAM. 100-OUT-1. EXHIBIT TEST-1(TEST-2). COMPUTE TEST-1(TEST-2) = TEST-2.

Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.