A plethora of articles and textbooks deal with the issue of prototyping in general. If you are one of those poor individuals who were abducted by aliens and were unable to keep abreast of the advances in the RPG language, you may be unfamiliar with the power of the P-spec. If so, I recommend that you go to one of the many resources available to you. One of my favorite resources is the IBM Redbook Who Knew You Could Do That with RPG IV? A Sorcerer's Guide to System Access and More (SG24-5402-00). This Redbook, compiled by a consortium of RPG gurus, including Susan Gantner and Jon Paris, does a marvelous job of detailing the power of RPG IV. Among the topics covered is prototyping. If you're not up to speed on this topic, get this Redbook and learn it...now!
The intent of this article is neither to focus on the P-spec nor to instruct on the nuances of creating procedure prototypes. Instead, the focus is on one specific area of prototyping: parameter prototyping. Were you aware that your PLISTs can be replaced by prototypes that not only are less cumbersome to understand but are more efficient? Probably not.
Over the past several years, I have been repeatedly disappointed by the lack of effort to advocate the use of the prototype in place of the PLIST. Those of us who are instructors need to do a better job of discussing the PLIST prototyping. Even the IBM Certification Tests for RPG IV still use *ENTRY PLIST in some of their test examples (although, in fairness, I must admit that there are also questions that test the candidates' abilities with prototyping). This should not be a case of "do as I say not as I do." Let's all do it, and we will all learn to do it the right way!
With that said, let's proceed into the world of parameter prototyping!
Here's a simple example: Figure 1 is a program that calls the program in Figure 2. This is a program that computes the area and returns a value. For the moment, let's set aside the issue of whether this second program should be a program or subprocedure for the sake of studying parameter prototyping on a basic level.
|
Figure 1: Program USEMATH calls program COMPAREA with parameters.
|
Figure 2: Program COMPAREA is used to compute and pass back area.
Amazingly simplistic program, isn't it? So why should we use prototyping instead of *ENTRY PLIST? Several reasons actually. One that ought to scare you is that *ENTRY PLIST is unsupported in the V5 free-format RPG IV. Not convinced? Well, how about the ease of conversion? Or perhaps the idea that compiler checking may catch program errors before runtime? Hopefully, one of these three things (or all of them) will grab you.
Free-format RPG IV does not support a number of our favorite old op codes. Get used to it. Parsing the old PLIST is complicated, and IBM wisely chose to simply not do it since you have the advantage of prototyping before you. Instead of a multi-line *ENTRY PLIST, there is a multi-line parameter prototype in the calling program and prototype interface in the associated called program. The calling program, USEMATH above, suddenly is replaced with the code in Figure 3:
|
Figure 3: This is the updated USEMATH program.
Wasn't that easy? The key to the conversion is the EXTPGM parameter of the prototype definition. It identifies the program that will be called whenever a Call to Prototyped Program (CALLP) is made. With the prototype in place, the remainder of the conversion was easier than slicing hot butter. (Note: You may see CALLP referred to as Call to Prototyped Procedure. This is also valid. The difference in definition is whether the EXTPGM parameter is used.)
So, if the new USEMATH program was that easy to convert, then the COMPAREA program must be the difficult one, right? Wrong! In fact, all you need to do is copy your procedure prototype from USEMATH to COMPAREA, change the 'PR' to a 'PI' to make it a procedure interface, and place it where the lines for your *ENTRY PLIST existed. Confused? Well, look at Figure 4 to end your confusion!
|
Figure 4: This is the new COMPAREA program.
Could a conversion be more easy? What's more, this method of passing parameters ensures compiler checking and portability because of the flexibility of prototype usage. If you store your prototypes in source files that are placed in your source as /COPY routines, you decrease the chances of maintenance nightmares. One source location translates into one source for maintenance.
How does the prototyping increase the reliability of the parameters? First, if you look at the CONST keyword used in the COMPAREA program of Figure 4, this is insurance for our variable values. The CONST keyword ensures that parameters are passed by value, not address. Thus, they are in effect "input only" parameters. Notice that the Area parameter did not contain a CONST keyword with it. This variable value will be affected by the program call.
And last but not least, what happens if someone passes a 10-position integer into program COMPAREA from Figure 2? Well, you'll find out at runtime, won't you? And if it's an error message, what will you do about it then? Cancel the program in the middle of some production job that is halfway processed? Could have some serious repercussions here.
Because of the prototyping used by program COMPAREA in Figure 4, type checking will begin to occur at compile-time as the passed parameter types will be checked against the parameter prototype. During runtime, if an integer is passed into this program, it will be adjusted to fit to the required size. If there were two decimal places in the width and an integer value of 3 were transmitted, the interface would pass the value 3.00 in its place to the procedure. Isn't this wonderful?
Anyone using the V5 version of RPG IV will find that in order to code programs entirely in free-format, there will be no alternative around the methods discussed here. Prototypes must be used! Moreover, there's no reason not to! It's easy to accomplish, and an element of error checking will exist that simply is not there with the PLIST implementation. There is no reason that any RPG IV programmer should not be using prototyping to pass parameters...except for not being aware that such features exist.
These issues may seem like small things, but they are very important-- if for no other reason than to enforce concepts that IBM has already begun to introduce to the world at large. Programmers need to understand that parameter prototyping should be the preferred method of parameter passing. Until we all adopt these techniques, we risk major headaches.
If you still have questions regarding prototyping your own *ENTRY PLIST, I encourage you to write me at
Vincent B. Goldsby has been an IBM midrange systems professional since 1987. Presently, he is a freelance speaker/instructor on ILE, RPG IV, and Introductory Java. His most requested course is a 4-day comprehensive session entitled "Cameras, Lights, RPG IV!!!" Vincent can be reached for comments and questions at
Suggested Reference
IBM Redbook: Who Knew You Could Do That with RPG IV? A Sorcerer's Guide to System Access and More, Brian R. Smith, Martin Barbeau, Susan Gantner, Jon Paris, Zdravko Vincetic, Vladimir Zupka, ITSO, February 2000.
LATEST COMMENTS
MC Press Online