Summary -

In this topic, we explains about the REDEFINES with detailed examples.

The REDEFINES clause allows to use multiple data description (data item) entries to describe the same storage area. It allows to declare the multiple data items for a single storage area.

Syntax -

REDEFINES Syntax

data-name-t, FILLER

Identifies an alternate data item name for the data-name-s; data-name-t is the REDEFINING item. Neither data-name-t nor any of its subordinate entries can contain a VALUE clause.

data-name-s

Data-name-s is the REDEFINED item. The data-name-s declaration can contain a REDEFINES clause. The declaration for data-name-s cannot contain an OCCURS clause.

Rules -

  • A data item can redefine any number of times.
  • data-name-t or data-name-s should not contain an OCCURS DEPENDING ON clause.
  • data-name-t and data-name-s must have the same level in the hierarchy.
  • However, the level numbers may be different and need not be the same.
  • data-name-t or data-name-s can be defined with level numbers 01-49 or 77.
  • data-name-t or data-name-s should not be defined with level number 66 or 88.
  • data-name-t or data-name-s can be declared with any usage.
  • REDEFINES clause may or may not have PICTURE clause.
  • REDEFINING item length need not be same with REDEINED item length. REDEFINING field length may be is equal, less than or greater than the REDEFINED field.
  • Redefinition begins at data-name-t and ends when a level-number less than or equal to level number of data-name-t is encountered.
  • No data item should declare with the level-number lower than data-name-t and data-name-s level numbers can occur between data-name-t and data-name-s declarations.

Data items types can be changed during the redefinition. For example -

05 A PICTURE X(5).
05 B REDEFINES A.
   10 BA          PICTURE X(2).
   10 BB          PICTURE X(2).
05 C              PICTURE X(10).

REDEFINING data item can contain elementary data items during the REDEFINES. For example -

01 A PIC X(10).
01 B REDEFINES A PIC 9(10).

Data items can be redefined without changing the length of existing data item. For example -

01 A PIC X(10).
01 B REDEFINES A.
   02  B1 PIC X(5).
   02  B2 PIC X(5).

Data items can be redefined by changing the length of existing data item. For example -

01 A PIC X(10).
01 B REDEFINES A PIC X(10).

Data items can be redefined by changing the usage of existing data item. For example -

01 A PIC X(10).
01 B REDEFINES A PIC X(07).
01 C REDEFINES A PIC X(12).

Undefined results can occur when -

  • A redefining item is moved to a redefined item.
  • A redefined item is moved to a redefining.

For example -

01 A PIC 9(10) USAGE DISPLAY.
01 B REDEFINES A PIC 9(05) USAGE COMP-3.

Practical Example -

Scenario - Below example describes how the redefines used in COBOL programming.

Code -

REDEFINES Program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. REDEFINE.                                            
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
       01 WS-VAR                           PIC X(20).                   
       01 WS-REQ-VAR1   REDEFINES WS-VAR   PIC X(20).                   
       01 WS-RLE-VAR2   REDEFINES WS-VAR   PIC X(10).                   
       01 WS-RGT-VAR3   REDEFINES WS-VAR   PIC X(30).                   
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           MOVE "MAINFRAME TECHNOLOGY"         TO WS-VAR.               
           DISPLAY "WS-VAR:        " WS-VAR.                            
           DISPLAY "WS-REQ-VAR1:   " WS-REQ-VAR1.                       
           DISPLAY "WS-RLE-VAR2:   " WS-RLE-VAR2.                       
           DISPLAY "WS-RGT-VAR3:   " WS-RGT-VAR3.                       
           DISPLAY " ".                                                 
                                                                        
           MOVE "MAINFRAME APPLICATION SYSTEM" TO WS-RGT-VAR3.          
           DISPLAY "WS-VAR:        " WS-VAR.                            
           DISPLAY "WS-REQ-VAR1:   " WS-REQ-VAR1.                       
           DISPLAY "WS-RLE-VAR2:   " WS-RLE-VAR2.                       
           DISPLAY "WS-RGT-VAR3:   " WS-RGT-VAR3.                       
                                                                        
           STOP RUN.                                                    
**************************** Bottom of Data ****************************

Output -

REDEFINES Program Output

Explaining Example -

In the above example, WS-VAR is the individual variable. WS-REQ-VAR1, WS-RLE-VAR2, WS-RGT-VAR3 are the redefined variables on WS-VAR.

WS-REQ-VAR1 and WS-VAR having same length 20 and it can also redefines like below -

01 WS-REQ-VAR1   REDEFINES WS-VAR.

WS-RLE-VAR2 is redefines WS-VAR of length 10 that is less than WS-VAR length. So WS-RLE-VAR2 having the part of WS-VAR data.

WS-RGT-VAR3 is redefines WS-VAR of length 30 that is greater than WS-VAR length. So WS-RGT-VAR3 having the WS-VAR data plus another 10 bytes.

Initially, WS-VAR assigned with "MAINFRAME TECHNOLOGY" and the redefined variables WS-REQ-VAR1, WS-RLE-VAR2, WS-RGT-VAR3 also having the same data according to their definied lengths.

Later, "MAINFRAME APPLICATION SYSTEM" assigned to WS-RGT-VAR3. WS-VAR, WS-REQ-VAR1, WS-RLE-VAR2, WS-RGT-VAR3 also having the same data according to their definied lengths.

Note! If initializing REDEFINED or REDEFINING variable, can change the other variables data automatically. Because both variable points to same memory location.