TechTip: String Parameters: Is VALUE or CONST Better?

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times
When you are writing your own procedure and you want to have a parameter that will not be changed by the procedure, you have the choice of passing it by value (VALUE keyword) or "read-only reference" (CONST keyword).

If your procedure would work equally well with a VALUE or CONST parameter, use this rule of thumb: use VALUE for pointers and numeric values, and use CONST for arrays, structures, and strings.

For any large parameter, it's better to pass it by reference (CONST) than by value. Passing by reference requires just the 16-byte address to be copied by the system to the parameter area. Passing by value requires the entire length of the parameter to be copied to the parameter area.

Your goal should be to reduce the amount of data-copying that is required to pass your parameters.

Let's consider a few ways to handle a string parameter of 100 bytes, with a specific case of a passed value of 'abc'. There are four ways to code the prototype:

  1. 100A VALUE
  2. 100A CONST
  3. 100A VALUE VARYING
  4. 100A CONST VARYING


In the first case, the compiler must first take a 100-byte temporary, copy 'abc' to it, and then fill the rest with blanks. Then, the system will copy that 100 bytes to the parameter area.
Total data movement: 100 + 100 = 200 bytes

In the second case, the compiler will prepare the same temporary, but the system will only have to copy its address to the parameter area.
Total data movement: 100 + 16 = 116 bytes

In the third case, the compiler will take a 102-byte temporary, set the first two bytes to 3, and copy 'abc' to the next three bytes. Then the system will copy that 102 bytes to the parameter area.
Total data movement: 5 + 102 = 107 bytes

In the fourth case, the compiler will prepare the same temporary, but the system will only have to copy its address to the parameter area.
Total data movement: 5 + 16 = 21 bytes

The best solution for string parameters is CONST VARYING.

Barbara Morris is on the RPG Compiler Development team in the IBM Lab in Toronto. She can be reached at This email address is being protected from spambots. You need JavaScript enabled to view it..

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: