TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Redefines Condition


Redefines Clause:

Redefines clause is used to define a storage with different data description. If one or more data items are not used simultaneously, then the same storage can be utilized for another data item. So the same storage can be referred with different data items.

01 WS-NAME.
   05 WS-FIRST-NAME   PIC X(05)	VALUE 'KALAI'.
   05 WS-FILLER       PIC X(01) VALUE SPACES.
   05 WS-LAST-NAME    PIC X(05)	VALUE 'SRINI'.

01 WS-FULL-NAME	REDEFINES   WS-NAME   	PIC X(11).

Following are the details of the used parameters −

  1. WS-NAME is Redefined Item

  2. WS-FULL-NAME is Redefining Item

In above example WS-TOTAL-NAME will use same memory area allocated to WS-NAME. So Ws-TOTAL-NAME will have 'KALAI SRINI'


Let us see another example below,

01 WS-OLD PIC X(10).

01 

WS-NEW1 REDEFINES WS-OLD PIC 9(8).

01

WS-NEW2 REDEFINES WS-OLD PIC A(10).

Following are the details of the used parameters −

  1. WS-OLD is Redefined Item

  2. WS-NEW1 and WS-NEW2 are Redefining Item

Tips:

  1. Level number of Redefined Item and Redefining Item should be same.

  2. Redefines definition must be the next data description you want to redefine.

  3. A redefining item will always have the same value as a redefined item.

  4. REDEFINES clause cannot be used for level numbers 66 and 88.

  5. REDEFINES may not be used in a level 01 entry in the File Section.

  6. Number of characters need not be the same for Redefined Item and Redefining Item. Compiler generates warning if number of characters in Redefining item is greater than Redefined item.

  7. Several data items can redefine the same data item.


Example: Let's see another example,

IDENTIFICATION DIVISION.
PROGRAM-ID. CBLREDEF.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DESCRIPTION.
   05 WS-DATE1.
      10 WS-YEAR         PIC X(4) VALUE '2020'.
      10 WS-MONTH        PIC X(2) VALUE '08'.
      10 WS-DATE         PIC X(2) VALUE '20'.
   05 WS-DATE2 REDEFINES WS-DATE1 PIC 9(8).

PROCEDURE DIVISION.
   DISPLAY "WS-DATE1 : "WS-DATE1.
   DISPLAY "WS-DATE2 : "WS-DATE2.
   GOBACK.

When you compile and execute the above program it produces the following result −

WS-DATE1 : 20200820
WS-DATE2 : 20200820

Interview Question:

Difference between REDEFINES and RENAMES?

RENAMES clause is used for regrouping elementary data items and gives one name to it. REDEFINES clause allow you to use different data descriptions entries to describe same memory area.


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal