Problem solve Get help with specific problems with your technologies, process and projects.

RPG dynamic value calculation versus hard-coded values

iSeries RPG expert Kevin Ketlzer illustrates dynamic value calculation in this example on programming on the iSeries.

We all know that with few exceptions (e.g. yes/no field) hard-coded valid values shouldn't be used in a program. But how often do you blatantly hard-code processing limits into your RPG programs? The array has 10 positions, so you start a counter at one and write a do loop that runs until it is 10. The warehouse field is defined as P(2,0) in one file and as P(3,0) in another, so you check if the field is greater than 99 and raise an error.

This works until the conditions change. For example, what if the array needs to be changed to 15 positions long? First, you need to change the array definition to 15. Then you have to scan the code, find every time you use it and change the limit to 15. But what if other arrays were defined as 10 positions because this one was defined as 10 positions? You will need to track them down and change them as well.

By defining fields LIKE(other field) and using BIFs like %LEN, %DECPOS, %SIZE and %ELEM, you can create code that will automatically adjust to the changed size. Yes, it means working harder initially, but in the end it will make your life easier when the situation changes. Let me give you an example of dynamic value calculation.

Converting a number to a hexadecimal value string using RPG
This is not a good program, but it is a simple-to-create, good example of dynamic value calculation. It implements a solution to convert a number to a hexadecimal value string. But it is clumsy, relatively slow and uses brute force. The true (read: better) way to convert a number to a hexadecimal value string is to prototype and call the 'C' api CVTHC.

Since this is a compiled program, the PR and PI define the program's entry parameters. By defining the values on the PI explicitly and the values on the PR like the PIs, this allows one set of values to be altered to change the entry parameters. NOTE: Names on the PR are irrelevant but necessary for proper definition.

The hexa data structure is defined to contain the return value as it is being calculated, and to provide a default error value if conversion is not possible. It is defined as containing an array that is filled with "-", which is the error value. The length of the array is defined as the length of the entry parameter return value hex_out. No matter what hex_out is changed to, hexa will contain the same number of characters. In addition, when the program starts it will contain a "-" in each position.

The program checks if the number of hexadecimal positions can store the number passed in, processes its way through those hexadecimal positions and calculates the value of each position (for four char hexadecimal output, the first position is 16^3 for each increment) all based on the length of the hex_out return value.
RPG screenshot of hexadecimal positions, defined parameters for iSeries

Here are the results of calling the program:
RPG hexadecimal positions, defined parameters for iSeries results

Modifying the program

Now to change the program to return a five-character hexadecimal value string, only these two lines need to be changed and the program recompiled.
Dynamic value calculation changed lines

Here are the results of calling the new program:
modified program

ABOUT THE AUTHOR: Ketzler has more than 12 years of Information Systems experience in the iSeries environment and is currently a Sr. Staff Consultant at Affiliated Resource Group. He is an IBM Certified Specialist in AS/400 RPG IV Programming and AS/400 RPG IV Development and can be reached via e-mail at

Dig Deeper on RPG iSeries programming