Varying length parameter lists for external procedures and functions

To determine the number of parameters passed to an ILE service program, external procedures and functions written in ILE RPG can use the %PARMS built-in function and all ILE languages can use the ILE _NPMPARMLISTADDR built-in function.

When a procedure within an ILE service program is written to accept a varying number of input parameters, SQL procedures and functions that call the program set the operational descriptor to indicate the parameter count. This number can be relied on by the ILE service program to determine the parameter count.

In the following example, two SQL functions are defined to use the same entry point name in an RPG service program, each passing a different number of parameters. This same service program could be called from many other ILE programs. The ILE RPG %PARMS built-in function is used to determine how many parameters are passed.

To avoid the need for additional SQL parameters, using PARAMETER STYLE GENERAL is recommended on the routine definitions.
CREATE FUNCTION RFUNC1 (P1 CHAR(5))
    RETURNS INT
    LANGUAGE RPGLE
    PARAMETER STYLE GENERAL
    EXTERNAL NAME 'TSTLIB/TSTSRVPGM(RPGPGM1)'

CREATE FUNCTION RFUNC2 (P1 CHAR(5),
                        P2 CHAR(10))
    RETURNS INT
    LANGUAGE RPGLE
    PARAMETER STYLE GENERAL
    EXTERNAL NAME 'TSTLIB/TSTSRVPGM(RPGPGM1)'
In the RPG code, the number of parameters can be obtained by using the %PARMS built-in function.
DCL-PROC RPGPGM1 EXPORT;
DCL-PI *N INT(10);
 P1 CHAR(5)  CONST OPTIONS(*NOPASS);
 P2 CHAR(10) CONST OPTIONS(*NOPASS);
END-PI;
  DCL-S nbr INT(10);
  DCL-S result INT(10);
  nbr = %PARMS;
  // logic can use the number of parameters to determine how to calculate the result
  RETURN result;
END-PROC;

For other ILE languages, this information is available by using the _NPMPARMLISTADDR built-in function.