Call Programs in ILE RPG, RPG IV
In RPGLE we can call some other program from within any program. To call an external program we use the opcode ‘CALL’. This opcode takes one mandatory and one optional (Total 2) parameters. The mandatory parameter is the name of the program to be called. The name of the called program is specified as the factor 2.
Example to call an external RPG program
//First Program (The Caller) C 'PGM1' Dsply C Call 'PGM2' C 'PGM1' Dsply C Return //Second Program (The Callee) C 'PGM2' Dsply C Return
Compile the two programs as PGM1 and PGM2 respectively. The output of the above program is as given below.
DSPLY PGM1 DSPLY PGM2 DSPLY PGM1
The above example shows how an RPG program can call another RPG program. Here it should be noted that the name of the program must be in Capital Letters. Otherwise an error similar to "Cannot resolve to any object" will be thrown by the system.
Behind the scene
When the system executes the Call statement of the above program, it takes a note of this. It stores somewhere that PGM1 Called PGM2. This log is called the Call Stack or Program Call Stack. After calling the PGM2 the OS forgets about PGM1 and executes the called program. When the PGM2 ends, The system checks its log to see who had called it. It then continues with the caller program PGM1 from the statement just after the 'Call' statement.
To transfer control completely to the called program and force the OS not to log specific program call, Use the TRFCTL (Transfer control).
OK, The above program simply calls the program specified within the quotes. As we have seen in the entry parameters sections, we can pass arguments also to the called program. It was really easy to pass arguments from command line in the example given. But how to pass arguments from inside an RPGLE program?
And argument can be passed from a program using the opcode "Parm". You are already familiar with this opcode since entry parameters so let us see the source code of the example.
Example to pass a parameter to an ILE RPG program (Interprogram Communication)
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++ D@Arg1 S 15P 5 Inz(12) ** CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result C @Arg1 Dsply C Call 'ENTRPGM2' C Parm @Arg1 C @Arg1 Dsply ** C Return //ENTRPGM2 source code DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++ D@Arg S 15P 5 ** CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result C *Entry Plist C Parm @Arg ** C Eval @Arg = @Arg * 2 C Return
The output of the program is as given below.
DSPLY 1200000 DSPLY 2400000
Please note the following important points
- The numeric parameter of a program call should be taken ideally as 15P 5.
- The name of the callee program is given in quotes. The name must be in capital letters.
- The parameter being passed may be initialized with a default value but the entry parameter can never be initialized.
- From the output of the above program run we can draw conclusion that the parameters are passed by reference. That is if the value of the parameter being passed changes in the called program, then that change is reflected in the callee program also. So, if you do not wish the parameter value being modified in any program call, then you should either save the value before call or use a different variable to pass the value.
Passing multiple parameters
If we want to pass multiple parameters, we can just add the param opcodes below the Call statement. But when we have to make several calls to the same program, the maintenace becomes difficult. If we have to add or comment out some parameters in future, we have to go to the individual program Call Statements and add or comment out the specific parametrs. Or say we have to change the order of parameters, or revert back the changes... We have one simple alternative to this way of passing parameters, The Parameter List. We can create a parameter list and pass this parameter list instead of passing individual parameters. The parameter list is nothing but a list of several fields as we have seen in the entry parameter list. These fields are to be populated before calling the program. The parameter list is specified in the result field and all the values of the parameters in parameter list are passed to the called program. The following example will explain you this.
Example to pass multiple parameters using parameter list
D@Arg1 S 15P 5 Inz(12) D@Arg2 S 10A Inz('Hello') ** C @ParmList PList C Parm @Arg1 C Parm @Arg2 ** C Call 'ENTRPGM2' @ParmList ** C Return
Notice the declaration of the parameter list. Also notice the Syntax of the call statements. Now Write one program which accepts these two parameters and displays their values.
Note:- The order and data type of the parameters being passed between programs must be exactly the same. Otherwise you might get the decimal data error. Also, while passing numeric data, it's advised to define them as 15P 5 and initialize them.