• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • A (Relatively) Easy Way to Process Parameters as an Array

    March 1, 2006 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.

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags:

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Maxava Migrate LiveTM minimizes disruption with seamless transitions. Upgrading to Power11, or cloud hosted system, Maxava has you covered!

    MIGRATE NOW

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    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

    TomorrowNow Upset at Quest User Group Omit Commas from Numeric Dates

    One thought on “A (Relatively) Easy Way to Process Parameters as an Array”

    • San says:
      December 1, 2020 at 5:58 am

      I was working on program i need to pass the argv from c program to rpgle. I came across your post and i found a easy way to do the parameter passing. I’m positing it here for some one.

      C Program:
      #include

      void C_PARMS(char **);
      int main(int argc, char **argv) {
      C_PARMS(argv);
      }

      RPGLE Module:
      H NOMAIN
      D c_parms Pr
      D * Dim(100)
      P c_parms B Export
      D Pi
      D P_Args * Dim(100)
      *
      D arg S 100A
      D i S 2S 0
      *
      /Free
      i = 1;
      DoW (P_Args(i) *NULL);
      arg = %Str(P_Args(i));
      i += 1;
      EndDo;
      Return;
      /End-Free
      P c_parms E

      Sorry for alignment.

      Reply

    Leave a ReplyCancel reply

Volume 6, Number 9 -- March 1, 2006
THIS ISSUE SPONSORED BY:

ProData Computer Services
Advanced Systems Concepts
WorksRight Software

Table of Contents

  • A (Relatively) Easy Way to Process Parameters as an Array
  • Omit Commas from Numeric Dates
  • Admin Alert: Moving Libraries Between i5/OS Partitions, Part 2

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Bob Code Assistant Gets Better Understanding Of IBM i Applications
  • IBM i In The Cloud And The Case For Reversing Replication
  • Guru: From Hiking Boots To Long-Term Memory
  • Inside IBM’s New PowerHA Cloud Offering, Full System Manager
  • IBM i PTF Guide, Volume 28, Number 39
  • Power Systems Turns In Another Great Quarter
  • Big Blue Is Right On Schedule With Fall 2026 IBM i Tech Refreshes
  • Guru: Environment Variables – Global Variables Wearing Hiking Boots
  • A New IBM i Entity Takes Shape: Power By Fortra
  • IBM i PTF Guide, Volume 28, Number 38

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle