Sign in

docs RPG Guide

Advantages of passing by value or read-only reference

Advantages of passing by value or read-only reference

Passing by value or read-only reference allows you to:

  • Pass literals and expressions as parameters.
  • Pass parameters that do not match exactly the type and length that are expected.
  • Pass a variable that, from the caller’s perspective, will not be modified.

One primary use for passing by value or read-only reference is that you can allow less stringent matching of the attributes of the passed parameter. For example, if the definition is for a numeric field of type packed-decimal and length 5 with 2 decimal positions, you must pass a numeric value, but it can be:

  • A packed, zoned or binary constant or variable, with any number of digits and number of decimal positions

  • A built-in function returning a numeric value

  • A procedure returning a numeric value

  • A complex numeric expression such as

          2 * (Min(Length(First) + Length(Last) + 1): %size(Name))

If the prototype requires an array of 4 elements, the passed parameter can be:

  • An array with fewer than 4 elements. In this case, the remaining elements in the received parameter will contain the default value for the type.
  • An array with 4 elements. In this case, each element of the received parameter will correspond to an element of the passed parameter.
  • An array with more than 4 elements. In this case, some of the elements of the passed array will not be passed to the received parameter.
  • A non-array. In this case, each element of the received parameter will contain the passed parameter value.