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
- The maximum size of the data that can be passed from one program to another program is 64K.
- The maximum size of the data that can be passed through the PARM operand is 100 bytes.
- Unique data name must be given to the variable in the Linkage Section for levels 01 to 77.
- Here, the data item or the variable should not consist of any values in the VALUE clause.
- 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,
- Call by Reference
- 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
Similar Reads
File Section in COBOL COBOL is a high-level programming language for business applications or business use. It was designed in 1959 by the Conference on Data Systems Language (CODASYL). It was primarily used in business, finance, and administration system for companies and governments. COBOL is still widely used in appli
6 min read
Input-Output Section in COBOL In COBOL, the INPUT-OUTPUT SECTION is a section of the program where you can define file-handling statements. These statements are used to read from or write to external files, such as data files or report files. The INPUT-OUTPUT SECTION follows the FILE SECTION and comes before the PROCEDURE DIVISI
6 min read
Next Sentence in COBOL In COBOL programming language we have three types of logical statements: 1. Sequence Statements: Used when we need to execute some piece of code repeatedly till some sequence. Like reading each data/record from some file till the end. 2. Selection Statements: Used when we want to flow the program ba
3 min read
Doubly Linked List in C A doubly linked list is a type of linked list in which each node contains 3 parts, a data part and two addresses, one points to the previous node and one for the next node. It differs from the singly linked list as it has an extra pointer called previous that points to the previous node, allowing th
13 min read
Coding Sheet in COBOL Every language needs an environment or platform to write codes. For example, in Java, we use notepad to write codes then compile them to run. Similarly, COBOL requires a coding sheet to write codes. COBOL is a business-oriented high-level language. It was developed for business, finance, and adminis
7 min read
String Handling in COBOL The string is the data type, used to represent the sequence of characters, which ends with /0. String provides much functionality which makes our programming easy when it comes to groups of chars, words, sentences, or lines. String Handling:  String handling is the process or method to handle the s
7 min read