Skip to main content

Return Number of Parameters (%PARMS) and Parameter Number (%PARMNUM) - IBM i

Return Number of Parameters (%PARMS)

Built-In Function %PARMS returns the number of parameters passed to the procedure where %PARMS is run. 

This BIF is useful when the procedure contains multiple optional parameters and if the procedure is called from different places with different number of parameters to identify the number of parameters passed. 

This is most commonly used to to handle the procedure logic based on the parameters passed only. 

Value returned by %PARMS is same as *PARMS in Program Status Data Structure if used in the main procedure. 

Let's have a look at the simple example to see how this works. 

%PARMS - RPGLE

In the above example, 
  • Lines - 5 to 9: Prototype declaration of the Sub procedure with one mandatory parameter, two optional parameters and one return value. We will see the code of procedure in a bit. 
  • Line - 18, 22 & 26: Calling the sub procedure with one mandatory parameter, two parameters and three parameters respectively. 
Let's have a look at the code for Sub procedure and see how the %PARMS work. 

%PARMS - RPGLE

In the above procedure, 
  • Lines 42 & 44: We are using %PARMS to determine the number of parameters passed. 
This is often used to handle the parameters (like initializing or making sure these aren't used in the logic etc) that are not passed and make sure the procedure works as expected. 

One thing to note here here, If the RTNPARM keyword is used return parameter would be considered as one of the (first) parameter and total number of parameters would be increased by 1. 

In the above example, If only one parameter is passed, %Parms would be 2 (by considering the return parameter). 

%PARMS with RTNPARM- RPGLE

In the above example, 
  • Line - 34: We are using RTNPARM keyword against the Procedure Interface. The same would need to mentioned in the Prototype as well. 
  • Lines - 42, 44 & 46: We are using %PARMS to check the number of parameters by considering the return parameter. 
    • %PARMS would be 2 if only one parameter is passed.
    • %PARMS would be 3 if two parameters are passed. 
    • %PARMS would be 4 if three parameters are passed. 

Return Parameter Number (%PARMNUM)

Built-In Function %PARMNUM returns the number of the parameter passed from the parameters list. 

Operand for this BIF should always be the parameter specified in the procedure interface. 

Below are the few important points to remember. 
  • %PARMNUM can only be used with the parameters defined in Procedure Interface (PI) and Parameter defined using PLIST cannot be used.
  • Parameter Name must be specified as it is defined in the PI. 
    • If the parameter is an array, Array should be used and not an Index. 
    • If the parameter is a data structure, Data structure should be used and not subfields.
    • If the parameter is a file, File should be used and not record format. 
  • If RTNPARM is used, Return value is considered as the first parameter and the other parameters would be considered next.
Let's have a look at an example without using RTNPARM first to understand how this works. 

PARMNUM - RPGLE

In the above example, 
  • Line - 34: Procedure Interface is defined without RTNPARM. 
  • Lines - 43, 46 & 49: %PARMNUM is used to get the parameter number passed. 
    • Only Parameter names from the Procedure Interface are to be used. 
  • Line - 52: %PARMS is used to return the total number of parameters passed. 
Similar to %PARMS, If we use RTNPARM total number of parameters would be increased by 1 and parameter number also would be increased by 1 as the return value is considered as first parameter. 

Let's add RTMPARM to the above procedure. 

PARMNUM with RTNPARM - RPGLE

By adding RTNPARM to the Procedure Interface, Number of the parameter and total number of parameters passed are increased by 1. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

Comments

Popular posts from this blog

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short form. Below i