25
Mon, Nov
1 New Articles

Practical RPG: Flexible Parameters, Part 1

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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)

 

/free                          

if %parms > 2;                

 wBegOrder = iBegOrder;      

 if %parms > 3;              

   wEndOrder = iEndOrder;    

   if %parms > 4;            

     wCustomer = iCustomer;  

   endif;                    

 endif;                      

endif;                        

 

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

Joe Pluta is the founder and chief architect of Pluta Brothers Design, Inc. He 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 Developing Web 2.0 Applications with EGL for IBM i, 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 This email address is being protected from spambots. You need JavaScript enabled to view it..


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 productivity using Eclipse.
List Price $59.00

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: