In modular programming, we need to call a procedure from different modules. To compile the module successfully we need to declare the prototype of the procedure in each module. Suppose, a procedure is being used in 10 modules and during maintenance we need to add/delete/modify a parameter. Now, that's a big problem. We will have to modify the prototype definitions in 10 + 1 = 11 modules! What's the way around?
No rewards to guess, copybook is the answer. However, copybook concept should be implemented beforehand, during the design time itself. In the example below we learn how to declare a prototype in a copybook.
Example of defining prototypes using copybooks.
The source code of copybook.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++
D W@Parm1 S 10A
**
D W@Prc1 Pr
D W@Parm1L Like(W@Parm1)
Here we defined the parameter as an independent variable. Then we defined the prototype parameter to be like this independent variable. Now we just copy this source in the desired module using simple copy program.
The source code of modules using W@Prc1.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++
/Copy QCPYLESRC,QBC
There's nothing significant here.
The source code of the module which defines the procedure W@Prc1.
/COPY QCPYLESRC,QBC
...
...
PName+++++++++++..T...................Keywords+++++
PW@Prc1 B
D W@Parm1L Like(W@Parm1)
...
...
The most significant advantage for me at least, is that I do not have to write the same definitions again and again. This task becomes extremely boring when we have several modules using a procedures and the procedure has several parameters sometimes around 20.
Tip: When you have several parameters say 10 or more, prefer using data structure as parameters. That way, your prototype definition will be reduced to one line instead of 10 or more.
So far we have seen two most common usages of copybooks.
- Declaring Export, Import variables
- Declaring Prototypes.
Next we will learn the difference between /COPY and /INCLUDE directives used to copy copybooks.