An IBMi RPG sub-procedure is a modular piece of code within an RPG (Report Program Generator) program on the IBM i Power System (the replacement for he IBM iSeries and even older AS400).
Here are some key points about sub-procedures:
Here's a simple example of a sub-procedure in RPG:
DCL-PR CalculateSum INT(10);
TERM1 INT(5) VALUE;
TERM2 INT(5) VALUE;
END-PR;
// Definition of the sub-procedure
DCL-PROC CalculateSum;
DCL-PI *N INT(10);
TERM1 INT(5) VALUE;
TERM2 INT(5) VALUE;
END-PI;
DCL-S Result INT(10);
Result = TERM1 + TERM2;
RETURN Result;
In this example, `CalculateSum` is a sub-procedure that takes two integer parameters and returns their sum.
What is the difference between the prototype and the procedure?
This question is one that trips up a lot of RPGLE newcomers (and even seasoned devs when juggling service programs and modular design). Here's the breakdown:
Concept | Prototype (dcl-pr) | Procedure (dcl-proc) |
|---|---|---|
What it is | A declaration of a procedure’s interface | The actual implementation of the procedure |
Purpose | Tells the compiler how to call the procedure | Contains the logic/code that runs when called |
Location | Typically in a header or /COPY file | Inside your RPGLE source, often after main() |
Includes | Procedure name, parameters, return type | Procedure name, interface (dcl-pi), and body |
Used for | Type checking, external calls, modular design. Only required if calling across modules or externally | Encapsulating reusable logic. Yes, if you want to define reusable logic. |
Prototype vs Procedure in RPGLE
Think of it like this:
Prototype = Blueprint. It says: “Here’s how you can call me.”
Procedure = Building. It says: “Here’s what I do when you call me.”
If the procedure is defined in the same module, you don’t have to declare a prototype but it’s good practice, especially for readability and reuse. And if you're exporting procedures from a service program, prototypes are mandatory.
