Practical RPG: Flexible Parameters, Part 1 PDF Print E-mail
Programming - RPG
Written by Joe Pluta   
Wednesday, 02 January 2013 00:00

Support MC Press - Visit Our Sponsors

Forums Sponsor



Search Sponsor



  Bookstore Sidebar Header




As we write more modular code, flexible parameter passing becomes ever more important, and this is the first article to address the nuances of the subject.


In order for two programs to communicate, they have to pass data. For most non-trivial applications (other than logging), this is typically a two-way conversation, if only for the called routine to return a completion code. Often, much more information is communicated, and typically this is done through a parameter list. I'm going to walk through a typical parameter list and explain how it works and how to take advantage of some of the built-in features of RPG.

An Example Parameter List

In this case, I'm going to provide the parameter list for a program that exports orders as a stream file. I'll start with the initial parameter list, which contains just three parameters, and then walk you through the organic growth of the list so you can see the issues that we face as programs evolve. Later in the article, I'll present a technique that addresses many of those issues.


In the first incarnation of the program, all I want to do is to specify the range of orders and the output file. In this initial case, the natural order of the parameter list looks like this:


d OrdExp       pi                                

d   BegOrder                     like(OHORDNUM)

d   EndOrder                     like(OHORDNUM)

d   OutputFile            128   varying      


I'm specifying the range as beginning and ending order numbers and the output file as a simple varying string. It's very straightforward. The problem comes when I want to add more features to the program. Let's say I want to add an additional filter field, the customer number, and a return value that indicates how many orders were exported. Here's what I end up with:


d OrdExp       pi                                

d   BegOrder                     like(OHORDNUM)

d   EndOrder                     like(OHORDNUM)

d   OutputFile            128   varying      

d   Customer                     like(OHCUSNUM) options(*nopass)

d   NumOrders                6s 0 options(*nopass)


Note that by adding the new parameters to the end, the code allows older programs to still call this one without being recompiled, provided that I check the number of parameters using the %parms BIF. With this first change, the code is still manageable: if the number of parameters is three, don't use the customer number and don't return the number of orders. However, as I add new parameters, the code starts to get unwieldy; for example, there's no clean way to get the number of orders processed without specifying the customer number. Not to mention that I can never call the application without passing an order range, even if I want all orders for a given customer (or simply a list of all orders, period).

Using a Standard Parameter List

Some of the problems can be avoided by starting out with a standard parameter list. I'm using the term "standard" very loosely; at best, it's my own personal standard and not one that even I always adhere to, but I think it provides a good stepping-off point. What I've done is divide the parameters into three groups: required parameters, returned parameters, and optional parameters. There's always a little art mixed in with the science, but it works for me:


d OrdExp       pi                                

d   OutputFile            128   varying      

d   NumOrders                6s 0

d   BegOrder                       like(OHORDNUM) options(*nopass)

d   EndOrder                     like(OHORDNUM) options(*nopass)

d   Customer                     like(OHCUSNUM) options(*nopass)


The OutputFile parameter is required. I must specify this; otherwise, it doesn't make sense to call the program in the first place. Since they must always be passed, required values occupy the first positions in the parameter list.


The number of orders is a returned value. Typically, the number of returned values changes less frequently than the number passed, so I put them next in the list. I usually need one of two varieties of return values: actual computed values (such as the number of records processed as in this example) or a completion code. In either case, while returned values may be ignored, the number of returned values rarely changes, so it makes sense to include them in the next section of the parameter list.


Finally, I include the optional parameters. Deciding which ones to position first is subjective, of course, but the rule of thumb is to include the values that are used most often as the first values and proceed to less-common parameters. That's often easier said than done, but it's the goal.

The Final Tweak

As noted, you can use %parms to see if a value has been passed. This can become unwieldy after a while, and it also can lead to difficult-to-find errors if you use the wrong constant when you're checking the value. What I do nowadays is to create a shadow work variable for every optional parameter, complete with a meaningful default value. Refer to the snippet of code following.


d OrdExp       pi                                

d iOutputFile            128   varying      

d iNumOrders                6s 0

d iBegOrder                     like(OHORDNUM) options(*nopass)

d iEndOrder                     like(OHORDNUM) options(*nopass)

d iCustomer                     like(OHCUSNUM) options(*nopass)


d wBegOrder    s                  like(iBegOrder) inz(0)

d wEndOrder    s                  like(iEndOrder) inz(*hival)

d wCustomer    s                  like(iCustomer) inz(0)



if %parms > 2;                

 wBegOrder = iBegOrder;      

 if %parms > 3;              

   wEndOrder = iEndOrder;    

   if %parms > 4;            

     wCustomer = iCustomer;  





As you can see, I've prefixed each of my input variable with the letter "i." That's one of my personal standards; you can use whatever naming techniques you choose. But by using a prefix letter, I can then create variables with a different prefix, in this case "w," to act as shadow variables. By default, they are initialized to values that make sense in my program (for example, the order number range is 0 to *HIVAL, which would include all orders). At program startup, I check the number of parameters, and if a parameter is passed, I use the passed value to override the initialized value of the shadow variable.


Then, throughout the program I simply use the shadow variable instead of the input parameter, and everything works out well. Certainly, this won't work in every case, but the technique is very serviceable as a starting point. For those wondering how I use the customer number, I just write code that matches the pseudo code "if customer number on the order equals the number specified, or the number specified is zero." In SQL, I use a clever little technique:


WHERE :wCustomer in (0, OHCUSNUM)


I'll leave that one as an exercise for the reader!


Have fun with this. I hope it makes your program-to-program calls a little easier. In a subsequent installment, I'll show you how this can be applied to service programs and how it can help make service programs work even in a mixed ILE and non-ILE environment!


Joe Pluta
About the Author:
Joe Pluta is the founder and chief architect of Pluta Brothers Design, Inc. and has been extending the IBM midrange since the days of the IBM System/3. Joe uses WebSphere extensively, especially as the base for PSC/400, the only product that can move your legacy systems to the Web using simple green-screen commands. He has written several books, including E-Deployment: The Fastest Path to the Web, Eclipse: Step by Step, and WDSC: Step by Step. Joe performs onsite mentoring and speaks at user groups around the country. You can reach him at


MC Press books written by Joe Pluta available now on the MC Press Bookstore.


Developing Web 2.0 Applications with EGL for IBM i Developing Web 2.0 Applications with EGL for IBM i

Joe Pluta introduces you to EGL Rich UI and IBM’s Rational Developer for the IBM i platform.

List Price $39.95
Now On Sale
WDSC: Step by Step WDSC: Step by Step
Discover incredibly powerful WDSC with this easy-to-understand yet thorough introduction.

List Price $74.95
Now On Sale
Eclipse: Step by Step

Eclipse: Step by Step

Quickly get up to speed and productive using Eclipse.

List Price $59.00

Now On Sale
Last Updated on Friday, 04 January 2013 10:34
User Rating: / 4