Open In App

Linkage Section in COBOL

Last Updated : 06 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The linkage Section in COBOL is used to declare the variables and it is used in calling the variable from another program. It is used in calling the variable or data from the called program by using the CALL statement. 

The linkage Section helps in sending the data to the calling program or receiving the data from the calling program. The declaration of a variable to the Linkage section is nothing but the group, individual or elementary variables. The linkage section is also used in receiving the data from JCL a  PARM operand (PARM is nothing but a parameter keyword).

Syntax :

DATA DIVISION.

LINKAGE SECTION.

[variable declaration]

Rules for Linkage Section 

  1. The maximum size of the data that can be passed from one program to another program is 64K.
  2. The maximum size of the data that can be passed through the PARM operand is 100 bytes.
  3. Unique data name must be given to the variable in the Linkage Section for levels 01 to 77.
  4. Here, the data item or the variable should not consist of any values in the VALUE clause.
  5. Each 01-level item should be defined or included in the PROCEDURE DIVISION USING the data-item name.

Passing the Parameters :

The parameters can be passed in two ways. They are,

  1. Call by Reference
  2. Call by Content

1. Call by Reference

Call by Reference is referred to changes made in the sub-program or called the program that will get reflected in the main program or calling program. 

Syntax:

CALL SUB-PROGRAM_NAME USING VARIABLE-ENTRIES. 

Below is the example for calling the sub-program from the main program. The code is the main program that calls the subprogram using the CALL statement. It has two data items or variables which are employee id and employee name. 

Example 1:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN-PROGRAM.
 
DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 EMPLOYEE-ID PIC 9(3) VALUE 102.
   01 EMPLOYEE-NAME PIC A(25) VALUE 'GEEKSFORGEEKS'.
 
PROCEDURE DIVISION.
   CALL 'SUB-PROGRAM' USING EMPLOYEE-ID, EMPLOYEE-NAME.
   DISPLAY 'I am Main Program'.
   DISPLAY 'Employee Id : ' EMPLOYEE-ID.
   DISPLAY 'Employee Name : ' EMPLOYEE-NAME.
STOP RUN.


The below code is the sub-program which is called by the main program. In the linkage section, it contains the same type of the data-items as declared in the main program. It changes the employee id of the employee under the linkage section using MOVE statement and it will be reflected in the main program when the code is executed.

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. SUB-PROGRAM.
 
DATA DIVISION.
   LINKAGE SECTION.
   01 LS-EMPLOYEE-ID PIC 9(3).
   01 LS-EMPLOYEE-NAME PIC A(25).
 
PROCEDURE DIVISION USING LS-EMPLOYEE-ID, LS-EMPLOYEE-NAME.
   DISPLAY 'I am Sub Program'.
   MOVE 110 TO LS-EMPLOYEE-ID.
EXIT PROGRAM.


Output :

I am Sub Program
I am Main Program
Employee Id : 110
Employee Name : GEEKSFORGEEKS

2. Call by Content 

Call by Content is referred to whatever changes made in the sub-program or called program will not get reflected in the main program or calling program. The content of the data in the main program remains unchanged.

Syntax:

CALL SUB-PROGRAM_NAME USING 

BY CONTENT VARIABLE-ENTRIES.

The below code is the main program that calls the subprogram using the CALL statement and BY CONTENT keyword. It has two data-items or variables which are employee id and employee name. 

Example 2:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN-PROGRAM.
 
DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 EMPLOYEE-ID PIC 9(3) VALUE 102.
   01 EMPLOYEE-NAME PIC A(25) VALUE 'GEEKSFORGEEKS'.
 
PROCEDURE DIVISION.
   CALL 'SUB-PROGRAM' USING BY CONTENT EMPLOYEE-ID, BY CONTENT EMPLOYEE-NAME.
   DISPLAY 'I am Main Program'.
   DISPLAY 'Employee Id : ' EMPLOYEE-ID.
   DISPLAY 'Employee Name : ' EMPLOYEE-NAME.
STOP RUN.


The below code is the sub-program which is called the main program. The linkage section, it contains the same type of the data-items as declared in the main program. It changes the employee id of the employee under the linkage section using the MOVE statement but it will not be reflected in the main program when the code is executed due to the usage of BY CONTENT in the main program.

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. SUB-PROGRAM.
 
DATA DIVISION.
   LINKAGE SECTION.
   01 LS-EMPLOYEE-ID PIC 9(3).
   01 LS-EMPLOYEE-NAME PIC A(25).
 
PROCEDURE DIVISION USING LS-EMPLOYEE-ID, LS-EMPLOYEE-NAME.
   DISPLAY 'I am Sub Program'.
   MOVE 110 TO LS-EMPLOYEE-ID.
EXIT PROGRAM.


Output :

I am Sub Program
I am Main Program
Employee Id : 102
Employee Name : GEEKSFORGEEKS


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads