Microsoft KB Archive/66012

Define Variables in LINKAGE Only If They Are Passed Parameters

PSS ID Number: Q66012 Article last modified on 11-07-1990

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

Summary: With Microsoft COBOL versions 3.00, 3.00a, and 4.00, any data items defined in the LINKAGE SECTION of a program must also be listed in the PROCEDURE DIVISION USING header. That is, data items can be declared only in the LINKAGE SECTION if they are actually passed to the program as parameters of a CALL or CHAIN statement in another program. If a data item is defined in the LINKAGE SECTION of a program and it is not a passed parameter, the results will be unpredictable. However, one known problem exists. If such a program passes multiple data items illegally defined in that program’s LINKAGE SECTION to another program via the CALL statement, the called program will not be able to access each parameter properly. Specifically, if the called program makes a change to any of the parameters, the change will affect all of the parameters. This behavior occurs under DOS or OS/2 real mode. Under OS/2 protected mode, the same program causes a protection violation. This information applies to Microsoft COBOL Compiler versions 3.00 and 3.00a for MS-DOS and MS OS/2 and to Microsoft COBOL Professional Development System version 4.00 for MS-DOS and MS OS/2.

More Information: The following two sample programs demonstrate the problem. MAIN.CBL passes two data items illegally declared in its LINKAGE SECTION to SUB.CBL. SUB.CBL modifies only the first parameter, assigning it the letter “A”. But when MAIN.CBL displays the data items after the CALL, both are set to “A”. To produce the correct behavior, the “LINKAGE SECTION.” line in MAIN.CBL must be changed to “WORKING-STORAGE SECTION.”. This modification is required because the data items that MAIN.CBL passes to SUB.CBL are created in MAIN.CBL and are not passed as parameters to it. Note that this behavior occurs when MAIN.CBL and SUB.CBL are linked together statically or dynamically. The behavior also occurs if the code for SUB.CBL is nested inside of MAIN.CBL (using the NESTCALL directive). To compile and link the programs in COBOL 3.00/3.00a for DOS or OS/2 real mode, enter the following at the DOS command prompt: cobol main; cobol sub; link main sub; To compile and link the programs in COBOL 3.00/3.00a for OS/2 protected mode, enter the following at the OS/2 command prompt: pcobol main; pcobol sub; link /nop /nod main sub,,,pcobol doscalls; The compile lines for COBOL PDS 4.00 in DOS (or OS/2 real mode) and OS/2 protected mode are the same as for DOS or OS/2 real mode with COBOL 3.00 and 3.00a. However, the link lines are different for COBOL 4.00 as follows: For COBOL 4.00 in DOS or OS/2 real mode: link /nod main sub,,,coblib cobapi; For COBOL 4.00 in OS/2 protected mode: link /nod main sub,,,coblib os2;

MAIN.CBL
$SET ANS85 IDENTIFICATION DIVISION. PROGRAM-ID. MAIN. DATA DIVISION. LINKAGE SECTION. 01 Var1  PIC X.   01 Var2   PIC X.   PROCEDURE DIVISION. CALL &quot;SUB&quot; USING Var1, Var2. DISPLAY Var1, Var2. STOP RUN.

SUB.CBL
$SET ANS85 IDENTIFICATION DIVISION. PROGRAM-ID. SUB. DATA DIVISION. LINKAGE SECTION. 01 Var1  PIC X.   01 Var2   PIC X.   PROCEDURE DIVISION USING Var1, Var2. MOVE &quot;A&quot; TO Var1. EXIT PROGRAM. Copyright Microsoft Corporation 1990.