How do you pass parameters between CL and RPG programs?

Passing parameters between CL (Control Language) and RPG (Report Program Generator) programs in IBM i is a common requirement when you need to integrate these two languages. Here's how you can achieve this:

1. Defining Parameters in RPG:

  • In your RPG program, you need to define the parameters that you expect to receive from the CL program.
  • You do this using the parameter definition specifications (D specs) in your RPG code.
  • You specify the data type, length, and other attributes of each parameter.

Example :

**FREE
ctl-opt dftactgrp(*no) actgrp(*caller);

dcl-s  Name            char(20)   ;
dcl-s  Age             zoned(3)   ;
dcl-s  Salary          packed(7:2);

dcl-pi  *n                
   Name             like(Name)   ;
   Age              like(Age)    ;
   Salary           like(Salary) ;
end-pi;

// ... rest of your RPG program logic ...

 

In this example :

  • Name is a character parameter of length 20.
  • Age is a zoned numeric parameter of length 3.
  • Salary is a packed numeric parameter with 7 digits and 2 decimal places.

2. Calling the RPG Program from CL:

  • In your CL program, you use the CALL command to invoke the RPG program.
  • You pass the parameters to the RPG program using the PARM parameter of the CALL command.
  • The order of the parameters in the PARM list must match the order of the parameters defined in the RPG program.

Example :

PGM        /* CL program to call the RPG program */

             DCL        VAR(&NAME) TYPE(*CHAR) LEN(20) VALUE('John Doe')
             DCL        VAR(&AGE) TYPE(*CHAR) LEN(3) VALUE('30')
             DCL        VAR(&SALARY) TYPE(*CHAR) LEN(9) VALUE('000500.00')

             CALL       MYLIB/MYRPGPGM PARM(&NAME &AGE &SALARY)

             ENDPGM

 

In this example :

  • We declare CL variables &NAME, &AGE, and &SALARY and assign values to them.
  • We then call the RPG program MYLIB/MYRPGPGM and pass these variables as parameters using the PARM parameter.

Important Considerations:

  • Data Type Compatibility: Ensure that the data types of the parameters in the CL program match the data types defined in the RPG program. You might need to perform data type conversions if they are not compatible.
  • Parameter Lengths: Pay close attention to the lengths of the parameters. If the lengths don't match, you might encounter errors or unexpected results.
  • Passing Numeric Values: When passing numeric values, you might need to use the %BIN or %DEC built-in functions in CL to ensure that the values are passed correctly.
  • Passing Character Values: When passing character values, ensure that they are enclosed in single quotes in the PARM list of the CALL command.

Best Practices:

  • Use descriptive parameter names: This makes your code easier to understand and maintain.
  • Validate input parameters: In your RPG program, check the values of the input parameters to ensure that they are valid.
  • Handle errors gracefully: If any errors occur during parameter passing, handle them appropriately in your RPG program.