Skip to Content

May I create my own type definitions and reuse them?

Estimated Reading Time: 1 Minutes

It is so desirable to create a simple or complex type definition that may be consistently and easily reused with other variables without having to copy paste the contents of the original one.

The TYPEDEF clause in the data description is the answer to this.

Consider the following example of a record that holds a time structure that can be reused with many time type variables.
You may create the type definition in the working-storage like this:

    01 td-time      typedef.
       05 ws-hrs    pic 99.
       05 filler    pic x value ":".
       05 ws-mins   pic 99.
       05 filler    pic x value ":".
       05 ws-secs   pic 99.

Then you may define the actual variables based on that definition, like this:

    01 init-time    usage td-time.
    01 end-time     usage td-time.
    01 first-time   usage td-time.

In this way, every time a variable will end up containing the 3 children fields: ws-hrs, ws-mins and ws-secs.

And you may use them on the procedure division as the following snippet demonstrates:

    move 13 to ws-hrs  of init-time
    move 20 to ws-mins of init-time
    move 40 to ws-secs of init-time
 
    move init-time     to end-time
    add 5 to ws-hrs    of end-time
    add 3 to ws-mins   of end-time

    move end-time      to first-time
    add 4 to ws-mins   of first-time
    add 6 to ws-secs   of first-time

    display "Init  time: " init-time  x"0d0a"
            "End   time: " end-time   x"0d0a"
            "First time: " first-time x"0d0a"

That would display the following on the console:

    Init  time: 13:20:40
    End   time: 18:23:40
    First time: 18:27:46
May I create my own type definitions and reuse them?
  • Attached Files
  • typedef.cbl (1.01 KB) 22