Summary -

In this topic, we described about the Linkage Section with detailed example.

LINKAGE SECTION defines the data items to receive the data from called program to the current program by using CALL. LINKAGE SECTION also defines the data items to receive the data from JCL through PARM operand.

LINKAGE SECTION declares the data items to make the data available from another program or to access the data external to the program.

LINKAGE SECTION mainly used in subprograms where the data receive/send from main program/calling program by declaring the group/individual data items.

The maximum length of the data can pass from program to program is 64K but recommended to pass around 24K.

If data items declared to receive the data from PARM, two bytes(s9(4) COMP) should be reserved to store the length of the data in additional to the other fields declaration.

The maximum length of the data can pass through PARM parameter is 100 bytes. The data items should match the PIC clause declaration in both calling and called programs.

A data-name defined at the 01 or 77 level in the Linkage Section must be unique and the elementary data-names may not be unique. The data items should not have a VALUE clause.

Syntax -

[DATA DIVISION.]
[LINKAGE SECTION.]
	[data-item-description-entries.]

Practical Example -

Scenario - Below example describes how the LINKAGE section is used to pass the data between the programs.

Main Program -

Main program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. MAINPROG.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
       01 WS-VAR.                                                       
          05 WS-IP1          PIC 9(02).                                 
          05 WS-IP2          PIC 9(02).                                 
          05 WS-RESULT       PIC 9(04).                                 
          05 WS-CALLING-PROG PIC X(08) VALUE "SUBPROG".                 
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           ACCEPT WS-IP1.                                               
           ACCEPT WS-IP2.                                               
           CALL WS-CALLING-PROG USING WS-IP1, WS-IP2, WS-RESULT.        
                                                                        
           DISPLAY "INPUTS:  " WS-IP1 ", " WS-IP2.                      
           DISPLAY "RESULTS: " WS-RESULT.                               
                                                                        
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Sub Program -

sub program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. MAINPROG.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
       01 WS-VAR.                                                       
          05 WS-IP1          PIC 9(02).                                 
          05 WS-IP2          PIC 9(02).                                 
          05 WS-RESULT       PIC 9(04).                                 
          05 WS-CALLING-PROG PIC X(08) VALUE "SUBPROG".                 
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           ACCEPT WS-IP1.                                               
           ACCEPT WS-IP2.                                               
           CALL WS-CALLING-PROG USING WS-IP1, WS-IP2, WS-RESULT.        
                                                                        
           DISPLAY "INPUTS:  " WS-IP1 ", " WS-IP2.                      
           DISPLAY "RESULTS: " WS-RESULT.                               
                                                                        
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Jcl -

LINKAGE section Jcl

Output -

LINKAGE section program Output

Explaining Example -

In the above example, MAINPROG is the main program and SUBPROG is the subprogram.

WS-IP1, WS-IP2 are the inputs passed from MAINPROG to the SUBPROG. SUBPROG receives the data into LN-IP1, LN-IP2 from MAINPROG, multiply those values and place the result into LN-RESULT.

SUBPROG returns the output LN-RESULT to MAINPROG, MAINPROG displays the result received from SUBPROG.