Tech Tip: Pass the Parameters

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

When you make program calls, the way you pass the parameters can impact the way the called program executes. For that reason, you need a good understanding of the parameter passing styles available. In this TechTip, I'll review the three parameter passing styles supported on the iSeries.

Style Choices

The three parameter styles supported by the iSeries are by reference, by read-only reference, and by value. It's important to understand that program calls that are not prototyped can only be called when passing parameters by reference. Prototyped program calls can support all three of these parameter styles. Let's take a look at the differences between each of these styles.

By Reference

This parameter passing style is generally used when the called procedure or program needs to modify the parameter value. This is because this parameter passing style actually passes the address of the value being passed, so any updates to the parameter within the called program or procedure update the value in the calling program directly. As a result, the field formats used when calling a program or procedure by reference must match those used by the called procedure or program exactly. This style is not only the style used by non-prototyped program calls but is actually the default used by ILE RPG. Therefore, this is the style that is used if no parameter passing style is explicitly defined on the procedure prototype. No keyword needs to be specified on the procedure prototype to select this parameter style. It's also important to remember that this style should always be used if the procedure to be called needs to modify the value of the parameter.

By Read-Only Reference

An alternative to passing parameters by reference is to pass parameters by read-only reference. This parameter passing style can be used with prototyped calls to allow you to specify parameter values using fields whose definitions do not exactly match the format expected by the calling program. To pass parameters by read-only reference, specify the CONST keyword on the parameter definition within the prototype definition. This style also allows you to specify complicated expressions or literals as parameter values. This means that the following free-form ILE RPG statement would be completely acceptable if the parameter passing style is defined as read-only reference:

     C/Free
        MyProc((12 * MthlySalary) – (Deductions)) ;


When passing parameters by read-only reference, it's important that the called program honor the "read only" state of the parameter. Otherwise, if the called program is written in a language that does not use prototypes, there's no way to ensure that the parameter will not be modified.

By Value

When passing a parameter by value, the value itself is passed, rather than a reference to the parameter from the calling program. With this style, the prototyped call can accept complex expressions, including literals and results from functions. This style also allows the passing of parameters that are not defined as exactly the same data type or length used in the called program. When parameters are passed by value, the called procedure can modify the passed parameter; however, the resulting value will never be returned to the calling program. This differs from passing by read-only reference in that even if the called program or procedure modifies the value, the calling program will not see the result.

What's Your Style?

When choosing a parameter passing style, you must consider a few factors. First and foremost, what style does the calling program expect? For example, if the called program is coded to expect to update a passed parameter, the prototype should be defined to pass the parameter by reference. If the called program will not be updating the parameter value or does not expect to return the updated value, calling by read-only reference or by value are the best choices. When passing large parameters, passing by read-only reference is generally the best choice. When passing a numeric or pointer parameter, in some cases passing by value will offer better performance.

No matter which parameter passing style you choose, understanding the options is an important first step.

Mike Faust is a Business Analyst for Invivo Corp. in Orlando, Florida. Mike is also the author of the books The iSeries and AS/400 Programmer's Guide to Cool Things and Active Server Pages Primer and SQL Built-in Functions and Stored Procedures. You can contact Mike at This email address is being protected from spambots. You need JavaScript enabled to view it..

Mike Faust

Mike Faust is a senior consultant/analyst for Retail Technologies Corporation in Orlando, Florida. Mike is also the author of the books Active Server Pages Primer, The iSeries and AS/400 Programmer's Guide to Cool Things, JavaScript for the Business Developer, and SQL Built-in Functions and Stored Procedures. You can contact Mike at This email address is being protected from spambots. You need JavaScript enabled to view it..


MC Press books written by Mike Faust available now on the MC Press Bookstore.

Active Server Pages Primer Active Server Pages Primer
Learn how to make the most of ASP while creating a fully functional ASP "shopping cart" application.
List Price $79.00

Now On Sale

JavaScript for the Business Developer JavaScript for the Business Developer
Learn how JavaScript can help you create dynamic business applications with Web browser interfaces.
List Price $44.95

Now On Sale

SQL Built-in Functions and Stored Procedures SQL Built-in Functions and Stored Procedures
Unleash the full power of SQL with these highly useful tools.
List Price $49.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • 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.

  • 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

  • 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: