18

May I ask, what is the usage of the keyword REDEFINES in COBOL? I can not understand the manual's definition.

What is the meaning of the following code?

 01 WS_CHARGE_TXT_8X                             PIC X(08) VALUE "10000000".  
 01 WS_CHARGE_NUM_8 REDEFINES WS_CHARGE_TXT_8X.  
     05 WS_CHARGE_8                               PIC 9(05)V9(03).  

Thank you!

Joachim Sauer
  • 278,207
  • 54
  • 523
  • 586
lamwaiman1988
  • 3,497
  • 15
  • 50
  • 87

3 Answers3

21

Basically Redefines reuses the spaces so in the above example WS_CHARGE_TXT_8X and WS_CHARGE_8 will point to the same block of memory. This allows you to look at a block of memory in different ways, in this case the variable can be viewed as a text PIC X and a signed numeric PIC S9. The -8X to -8 in the variable name is just stylistic to indicate the variable is being recast to another type or format to other programmers.

In the above example

  • the value of WS_CHARGE_TXT_8X is "10000000"
  • the value of WS_CHARGE_8 is of 10000.000.

If you moved 123.456 to WS_CHARGE_8 the value of WS_CHARGE_TXT_8X "00123456".

A more useful example is

  03 Birth-Date-YYYYMMDD    pic 9(8).
  03 filler redefines Birth-Date-YYYYMMDD.
     05 Birth-Date-YYYY     pic 9(4).
     05 Birth-Date-MM       pic 99.
     05 Birth-Date-DD       pic 99.

In this case you can access the whole date Birth-Date-YYYYMMDD or the year / month / day individually (Birth-Date-YYYY etc).

Bruce Martin
  • 9,845
  • 1
  • 24
  • 36
  • Quick question: Can't we just leave Birth-Date-YYYYMMDD? If we want to display who date, just do DISPLAY filler? – Programmer Oct 02 '13 at 08:00
  • @Programmer, I am a little unsure of your question, but you can certainly define Birth-Date-YYYYMMDD by itself. – Bruce Martin Oct 02 '13 at 22:25
  • Normally fillers with values are intended to be missed by INITIALIZE. Not good form for it to redefine the previous field as it seems redundant ? – mckenzm Mar 10 '15 at 21:11
  • @mckenzm **all** FILLERs are ignored by INITIALIZE, with a VALUE or not. – Bill Woodger Mar 10 '15 at 21:42
3

It is analogous to union in 'C'. It saves working storage space, and MOVE statements, and is also useful for transposing arrays of PIC(X), or accessing repeated fields as an array. In the OP's case a numeric "type" is being created for the char contents of the prototype field.

mckenzm
  • 1,001
  • 1
  • 8
  • 15
  • How does it save WORKING-STORAGE space when it is not limited to the WORKING-STORAGE SECTION? Why would you want/need to save WORKING-STORAGE space? It's probably about 40 years since that was a meaningful use of REDEFINES. How does it save MOVEs? "Prototype"? What does that mean in COBOL? – Bill Woodger Mar 10 '15 at 21:40
  • If it was not available as a feature, it would be necessary to define another area and move the contents. Think about this. Traditionally COBOL was not about getting main and using pointers. A prototype is the "original" upon which we model. In any language, (but particularly in railway modelling.) – mckenzm Mar 06 '17 at 04:34
1

A REDEFINES allows a different data description for storage which has already been defined.

That might be to allow entirely different data to be held there.

05  RL-AGENT-DATA.
...
05  RL-CUSTOMER-DATA REDEFINES RL-AGENT-DATA.
...

Or to give a description which allows a use of a part of the data, as in Bruce's example.

Or to use a piece of data that is the same, but for a different purpose:

05  INPUT-AMOUNT PIC X(10).
05  INPUT-AMOUNT-NUMERIC REDEFINES PIC 9(8)V99.

Whatever is in INPUT-AMOUNT it can be reported without problem, but only if it is a valid numeric (by testng it for NUMERIC), is INPUT-AMOUNT-NUMERIC used for some purpose.

Note that each 01 subsequent to the first under an FD is an "implicit REDEFINES".

Note also that items in the LINKAGE SECTION have the effect of "redefining" "something", even though if the address of the data is from a CALLing program, the definition is often the same as the original definition, and should usually at least match the PICtures of the original.

Bill Woodger
  • 12,702
  • 4
  • 35
  • 43