What is an RPG Sub-Procedure?

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.

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