A subprocedure is a procedure defined after the main source section.
A subprocedure differs from a cycle-main procedure in several respects, the main difference being that a subprocedure does not (and cannot) use the RPG cycle while running.
A subprocedure may have a corresponding prototype in the definition specifications of the main source section. If specified, the prototype is used by the compiler to call the program or procedure correctly, and to ensure that the caller passes the correct parameters. If not specified, the prototype is implicitly generated from the procedure interface.
Although it is optional to specify a prototype within the module that defines the procedure, it should not be considered optional when it is exported from the module, and the procedure will be called from other RPG modules. In this case, a prototype should be specified in a copy file and copied into the module that defines the subprocedure and into every module that calls the subprocedure.
Figure 5 shows a subprocedure, highlighting the different parts of it.
* Prototype for procedure FUNCTION
D FUNCTION PR 10I 0 1
D TERM1 5I 0 VALUE
D TERM2 5I 0 VALUE
D TERM3 5I 0 VALUE
P Function B 2
*-------------------------------------------------------------
* This procedure performs a function on the 3 numeric values
* passed to it as value parameters.
*
* This illustrates how a procedure interface is specified for a
* procedure and how values are returned from a procedure.
*-------------------------------------------------------------
D Function PI 10I 0 3
D Term1 5I 0 VALUE
D Term2 5I 0 VALUE
D Term3 5I 0 VALUE
D Result S 10I 0 4
/free
Result = Term1 ** 2 * 17
+ Term2 * 7 5
+ Term3;
return Result * 45 + 23;
/end-free
P E 6
Except for the procedure-interface definition, which may be placed anywhere within the definition specifications, a subprocedure must be coded in the order shown above.
No cycle code is generated for subprocedures. Consequently, you cannot code:
The calculation specifications are processed only once and the procedure returns at the end of the calculation specifications. See Subprocedure Calculations for more information.
A subprocedure may be exported, meaning that procedures in other modules in the program can call it. To indicate that it is to be exported, specify the keyword EXPORT on the Procedure-Begin specification. If not specified, the subprocedure can only be called from within the module.