Variable References Example


Scenario - Describes how the references used for data division names in COBOL programming.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. DREFDDN.
       AUTHOR. MTH. 

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01 WS-GROUP1.
          05 WS-VAR1     PIC X(10) VALUE 'MAINFRAMES'. 
          05 WS-VAR2     PIC 9(04) VALUE 2021.
       01 WS-GROUP2.
          05 WS-VAR1     PIC X(10). 
          05 WS-VAR2     PIC 9(04). 

       PROCEDURE DIVISION. 

           MOVE WS-VAR1 IN WS-GROUP1 
             TO WS-VAR1 IN WS-GROUP2. 
           DISPLAY "WS-GROUP1.WS-VAR1: " WS-VAR1 OF WS-GROUP1.
           DISPLAY "WS-GROUP2.WS-VAR1: " WS-VAR1 OF WS-GROUP2.

           STOP RUN.

Output -

WS-GROUP1.WS-VAR1: MAINFRAMES
WS-GROUP2.WS-VAR1: MAINFRAMES

Explaining Example -

In the above example:

  • WS-VAR1 and WS-VAR2 are elementary items declared under group items WS-GROUP1 and WS-GROUP2.
  • WS-VAR1 is not unique and should have reference while using in the program. So, WS-VAR1 under WS-GROUP1 refers as WS-VAR1 OF WS-GROUP1 or WS-VAR1 IN WS-GROUP1. Similarly, references should specify for WS-VAR2.