fhg
Volume 6, Number 9 -- March 1, 2006

A (Relatively) Easy Way to Process Parameters as an Array

Published: March 1, 2006

by Dominic Lefevre

In Printing from Qshell, Ted Holt lamented that there seems to be no useful way to treat parameter lists in RPG programs and procedures as arrays, as the C language does. This need arises for me every once in a while, and while I've never found a completely satisfactory way to handle it, I have found a way that works.

First, it might be good to provide an example in C, for those who do not know that language, in order to illustrate the advantage of treating parameters as an array. The following C program prints out the parameters that are passed to it.

#include <stdio.h>

main(int argc, char *argv[])
{
    int ndx;
    for (ndx = 1; ndx < argc; ndx++)
    {
       printf("Arg %2d is \"%s\"\n", ndx, argv[ndx]);
    }
    return(0);
}

Notice that the main function receives two arguments from the caller. The first one, argc, is an integer value that tells the number of parameters that were passed to the program. It is the C counterpart to RPG's %PARMS built-in function. The second one, argv, is an array of pointers to the parameters. The for loop presents a numbered list of the parameters. For example, suppose I call the program and pass it three parameters.

CALL MYCPGM PARM('a' 'bb' 'ccc')

This is the output I will see.

Arg  1 is "a"
Arg  2 is "bb"
Arg  3 is "ccc"

Now, think about this. How would you modify this C program if you wanted to pass four parameters to it? Five parameters? Ten parameters? Twenty? The answer is that you would not have to modify the program at all.

The RPG convention is different. Each parameter must be given its own name and must be referenced by that name, rather than by its position in a list. If you want to pass additional parameters, you have to modify the parameter list and add additional code to process the added parameters. I cannot change the requirement to modify the parameter list, but I can avoid the additional code that must process the extra parameters.

The following RPG program provides a simple illustration. I have designed it to process from zero to 12 parameters.

D MYRPGPGM        PR        EXTPGM('MYRPGPGM')
D  Action01       LIKE(Action) OPTIONS(*NoPass)
D  Action02       LIKE(Action) OPTIONS(*NoPass)
D  Action03       LIKE(Action) OPTIONS(*NoPass)
D  Action04       LIKE(Action) OPTIONS(*NoPass)
D  Action05       LIKE(Action) OPTIONS(*NoPass)
D  Action06       LIKE(Action) OPTIONS(*NoPass)
D  Action07       LIKE(Action) OPTIONS(*NoPass)
D  Action08       LIKE(Action) OPTIONS(*NoPass)
D  Action09       LIKE(Action) OPTIONS(*NoPass)
D  Action10       LIKE(Action) OPTIONS(*NoPass)
D  Action11       LIKE(Action) OPTIONS(*NoPass)
D  Action12       LIKE(Action) OPTIONS(*NoPass)
D MYRPGPGM        PI
D  Action01       LIKE(Action) OPTIONS(*NoPass)
D  Action02       LIKE(Action) OPTIONS(*NoPass)
D  Action03       LIKE(Action) OPTIONS(*NoPass)
D  Action04       LIKE(Action) OPTIONS(*NoPass)
D  Action05       LIKE(Action) OPTIONS(*NoPass)
D  Action06       LIKE(Action) OPTIONS(*NoPass)
D  Action07       LIKE(Action) OPTIONS(*NoPass)
D  Action08       LIKE(Action) OPTIONS(*NoPass)
D  Action09       LIKE(Action) OPTIONS(*NoPass)
D  Action10       LIKE(Action) OPTIONS(*NoPass)
D  Action11       LIKE(Action) OPTIONS(*NoPass)
D  Action12       LIKE(Action) OPTIONS(*NoPass)

D pAction         S               *
D Action          S            256A   BASED(pAction)

D pParms          S               *   DIM(12)
D N               S             10U 0

 /free
       pParms(01) = %ADDR(Action01);
       pParms(02) = %ADDR(Action02);
       pParms(03) = %ADDR(Action03);
       pParms(04) = %ADDR(Action04);
       pParms(05) = %ADDR(Action05);
       pParms(06) = %ADDR(Action06);
       pParms(07) = %ADDR(Action07);
       pParms(08) = %ADDR(Action08);
       pParms(09) = %ADDR(Action09);
       pParms(10) = %ADDR(Action10);
       pParms(11) = %ADDR(Action11);
       pParms(12) = %ADDR(Action12);

       DOU '0';
           N += 1;
           IF N > %ELEM(pParms) OR
              N > %PARMS() OR
              pParms(N) = *NULL;
                  LEAVE;
           ENDIF;
           pAction = pParms(N);
           // variable Action contains the current parm
       ENDDO;

My technique is to imitate the behavior of C programs. That is, I load an array of pointers--pParms--with the addresses of the parameters. The %AADR function returns a null value for any parameter that is not passed. To access the Nth parm, I set pointer pAction to the address loaded in the Nth element of the array. Since the Action variable is based on the pAction pointer, the Action variable provides me access to the parameter.

All of the following are legitimate CL calls to the RPG program.

CALL MYRPGPGM 
CALL MYRPGPGM (&VAR) 
CALL MYRPGPGM (&VAR &SOMEVAR) 
CALL MYRPGPGM (&VAR &SOMEVAR &ANOTHERVAR) 
CALL MYRPGPGM (&V1 &V2 &V3 &V4 &V5 &V6 &V7 +
               &V8 &V9 &V10 &V11 &V12) 

In fact, the CALL command can pass more than 12 parameters. However, the RPG program will ignore the additional parameters.

If I decide that the RPG program should accept more parameters, I must do the following:

  • Add the additional parameters to the parameter list
  • Change the dimension value of pParms
  • Add additional commands to load the addresses of the new parameters into the pParms array

But I do not have to add more calculation logic to handle the additional parameters.

A couple more points are in order. First, it is not necessary to use an array of pointers. I could have just as easily, or perhaps more easily, defined pParms as an array of characters. However, doing so would have eaten up much more memory. Using pointers makes for a smaller program.

Second, another way to pass an array of values to an RPG program is to create a CL command that includes a list parameter. However, this method is not always available. The Printing from Qshell article to which I referred earlier is a good example.



Sponsored By
PRODATA COMPUTER SERVICES

Get there faster with RSP!

 

RPG Server Pages is your iSeries Web solution for RPG programmers. RSP integrates HTML, Javascript, and RPG enabling you to visually create Web applications. The RSP editor is a WYSIWG Web page editor that is RPG-aware. It includes RPG source line prompting and the drag-and-drop of source code for instant productivity!

 

Contact us today at 800.228.6318 or sales@prodatacomputer.com
for your FREE 30-day licensed copy.

 

www.prodatacomputer.com



Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

nuBridges:  Leading provider of secure FTP on the iSeries
COMMON:  Join us at the Spring 2006 conference, March 26-30, in Minneapolis, Minnesota
BCD:  Try WebSmart - the easiest and most complete iSeries Web development tool

 


 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement