cobol redefines clause with examples

This topic covers the redefines clause usage, syntax, declaring and valid & Invalid redefines cases.
Redefines clause is used to share the common storage among the different data items/groups. This is useful to optimize the usage of working storage area.
1. This can be used at 01 level as well.
Ex:
01 VAR-1 PIC X(10) VALUE ‘MAHENDER’.
01 VAR-2 REDEFINES VAR-1 PIC X(5).
Here Var-1 is the redefined and Var-2 is the redefining item.
Redefining item should not contain a value clause where as redefined item can have a value clause as shown above.

2. One or more redefines are permitted
01 VAR-1 PIC X(10) VALUE ‘MAHENDER’.
01 VAR-2 REDEFINES VAR-1 PIC X(5).
01 VAR-3 REDEFINES VAR-2 PIC X(4).

Here Var-3 redefined Var-2, we can also use Var-1 instead of Var-2.
01 VAR-1 PIC X(10) VALUE ‘MAHENDER’
01 VAR-2 REDEFINES VAR-1 PIC X(5).
01 VAR-3 REDEFINES VAR-2 PIC X(8).
DISPLAY “VAR-1” VAR-1.
DISPLAY “VAR-2:” VAR-2.
DISPLAY “VAR-3:” VAR-3.

The output would be
VAR-1MAHENDER
VAR-2:MAHEN
VAR-3:MAHENDER
note that even we have redefined Var-3 of Pic X(8) with Var-2 of 5 bytes the var-3 value is of 8 chars not 5 chars.
Becoz it is a common space that is used by all the 3 variables, whatever the value is present it shows in all 3 fields.
But unacceptable results occur if we try to move Var-1 to Var-2 or vise verse.

3. Redefined and Redefining items may vary in the data types and lenghts.
01 a pic x(9)
02 b redefines a pic x(8)

4. Redefined Item(a) should not contain occurs clause, but it can be part of an occurs group item.
Ex:Valid case
Redefines can be the sub level of a occurs clause, but it can not redefine the item with occurs clause.
01 VAR-1.
05 VAR-2-GRP OCCURS 10 TIMES.
07 VAR2-A PIC X(5) VALUE ‘REDDY’.
07 VAR2-B REDEFINES VAR2-A PIC X(3).

Ex: Invalid case, this can not be done
01 VAR-1.
05 VAR-2-GRP OCCURS 10 TIMES.
07 VAR2-A PIC X(5) VALUE ‘REDDY’.
07 VAR2-B PIC X(3).
05 VAR-3-GRP REDEFINES VAR-2-GRP PIC X(50).
If we declare like that compiler throws an error ‘A definition of an object of a “REDEFINES” clause contained an “OCCURS” clause. The “REDEFINES” clause was discarded’.

5. Redefines in File Section.
Redefines is possible in the FD section of file control but it is the implicit redefinition. If we declare the multiple file declarations(more than one 01 level entries) in the FD, then the redefines applies implicitly i:e the first 01 level storage will be allocated to next level.

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami