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

$0.00 Raised:
$