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:

  • Definition: A sub-procedure is defined after the main source section of an RPG program. It is a self-contained block of code that performs a specific task
  • Independence: Sub-procedures are independent of the main procedure and other sub-procedures. They have their own local variables, which are not accessible outside the sub-procedure
  • No RPG Cycle: Unlike the main procedure, sub-procedures do not use the RPG cycle, which is a predefined sequence of operations that RPG programs typically follow
  • Prototyping: Sub-procedures often have prototypes, which are declarations that specify the sub-procedure's name, parameters, and return type. Prototypes help ensure that the sub-procedure is called correctly
  • Parameter Passing: Parameters can be passed to sub-procedures by value, and they can return values. This makes them useful for performing calculations or operations that need to return a result
  • Local Variables: Variables declared within a sub-procedure are local to that sub-procedure, meaning their values are not preserved between calls to the sub-procedure

Here's a simple example of a sub-procedure in RPG:

// Prototype for the sub-procedure
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;
END-PROC;

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.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>