Q: I know you can call an RPG program from CL, but I'd really like to call an RPG subprocedure instead. We recently started writing all our code as RPG subprocedures, and there are times when it would be useful to call it from CL. Is that possible?
A: It's not possible from the original version of CL, because that language runs in the OPM environment like RPG/400 programs do. However, starting with V3R1, IBM provides an ILE version of CL, included with the operating system. The ILE version of CL provides the Call Bound Procedure (CALLPRC) command, which you can use to call subprocedures.
Unlike RPG, the CL language did not change significantly when the ILE version was created. The CL syntax is exactly the same in ILE as it was in OPM. However, there are some notable differences:
The answer to your question, therefore, is to use ILE CL and the CALLPRC command, which lets you call a subprocedure and pass parameters. CALLPRC is similar to the traditional CALL command we've always had in CL, except that it calls a procedure and, of course, you have to bind to that procedure when you build your CL program.
Here's an example of an RPG subprocedure that you might want to call from CL:
D ORDER_checkPrice... D PR 10i 0 D ItemNo 8a const D Price 9p 2 const
This routine accepts two parameters: an item number that's 8A and a price that's 9,2 packed. It returns a four-byte integer. In CL, you'd call it as follows:
PGM
DCL VAR(&ORDER) TYPE(*CHAR) LEN(8)
DCL VAR(&PRICE) TYPE(*DEC) LEN(9 2)
DCL VAR(&RTNVAL) TYPE(*INT) LEN(4)
CALLPRC PRC('ORDER_CHECKPRICE') +
PARM((&ORDER *BYREF) +
(&PRICE *BYREF)) +
RTNVAL(&RTNVAL)
ENDPGM
As you can see, I have the same parameters defined in CL as the RPG is expecting to receive. I simply pass the parameters and receive the return value. It couldn't be much easier, could it?
For more information, try prompting the CALLPRC CL command and checking the help that runs behind it. Good luck!