If you wish to define a particular storage with a different data descriptions, then you can use COBOL Redefines clause. In addition, it is also a faster way to reinterpret the same data.
In this way, different data items can refer to the same memory location to reduce the memory space and the program is easier to maintain as well.
For instance, you can store a alphanumeric & a numeric data type at the same location using the Redefines in COBOL. Also, you can redefine alphanumeric to numeric using this clause.
There are many ways to use REDEFINES Clause. To illustrate, we are going to cover many examples here –
Here, WS-VAR1 is redefined to WS-VAR2. It does not mean that you cannot refer WS-VAR1, rather it means that WS-VAR2 shares the same memory location as WS-VAR1. So, WS-VAR2 is the redefining item while WS-VAR1 is redefined item.
JCL to run the above program
VALUE OF WS-ITEM1 IS – 4
VALUE OF WS-ITEM2 IS – 4
Rules for REDEFINES
- First, both the items i.e. redefined item as well as the redefining item must be at the same level.
The below example shows a wrong way of using Redefines because both the data items are at different level.
- Level Number 66 and 88 cannot be used for Redefines in COBOL as it is not allowed to redefine level numbers 66 and 88.
- The most common level numbers to use a REDEFINES clause are from 01 to 49 but Level number 77 can also be used.
- Obviously, you must NOT code the VALUE clause with a redefining item.
- In contrast, do not use the redefines clause in FILE SECTION with level number 01.
- REDEFINES may or may not have a picture clause.
- The value of Redefined item and REDEFINING item is always same when you use the REDEFINES clause.
OLD VALUE OF WS-ITEM1 IS – 4
OLD VALUE OF WS-ITEM2 IS – 4
NEW VALUE OF WS-ITEM1 IS – 8
NEW VALUE OF WS-ITEM2 IS – 8
- The redefined and redefining items will always have the same value at any point of time but the only exception is when the length of Redefined and Redefining items are different. In that case the value of data item which has lesser length might get truncated. Refer Rule 11 to understand more.
- Usage of the redefined data item can be different from the usage of the redefining item.
- You can redefine the redefined data item any number of times but you cannot redefine the redefining item.
The above code is right but the below code is wrong.
- The length of the REDEFINING item can be same or different than the length of the redefined field.
Here, ITEM-1 and ITEM-2 are numbers and both occupies the same length. Suppose, ITEM-1 is stored starting from 0 to 6 bytes in the memory then ITEM-2 will point to the same memory location starting from 0 to 6 bytes as well.
Similarly, ITEM-1 is alphanumeric while ITEM-2 is numeric. Suppose, ITEM-1 is stored starting from 0 to 10 bytes in the memory then ITEM-2 will point to the same memory location starting from 0 to 9 bytes only because its length is 9.
- The data type of the redefined item and redefining item can be same or different.
WELCOME TO TUTORIALBRAIN
BEST MAINFRAME TUTORIAL
- WS-ALPHANUM holds a value of TUTORIALBRAIN while WS-NUM has a value of TUTORIAL because it can hold 10 bytes only.
- Even though WS-NUM is declared as Numeric, it can still hold alphabets of length 8 because WS-ALPHANUM and WS-NUM refers to the same memory location.
- As WS-NUM is defined as Numeric but it has a value of TUTORIAL so you cannot use this variable in any arithmetic operation or logical comparisons else you will get SOC7 ABEND.
- OCCURS clause can also be used along with REDEFINES.
1 DAY OF WEEK – MONDAY
2 DAY OF WEEK – TUESDAY
3 DAY OF WEEK – WEDNESDAY
4 DAY OF WEEK – THURSDAY
5 DAY OF WEEK – FRIDAY
6 DAY OF WEEK – SATURDAY
7 DAY OF WEEK – SUNDAY
- The redefining item can also contain elementary items.
- Likewise, the redefined item can also contain elementary items.
DOB IS : 20200205
EXPIRY DATE IS : 20200205