How can we help you?

We have hundreds of highly-qualified, experienced experts working in 70+ technologies.

IBM i e-Book

A Developer’s Guide to Mastering IBM i Concepts

Report Program Generator (RPG)

Subroutines

A subroutine is a self-contained section of code within an RPG program that performs a specific task or set of tasks.
Subroutines in RPG are used to promote code reusability, modularity, and maintainability by encapsulating a particular functionality or calculation into a separate and callable unit.
RPG subroutines are defined using the EXSR (Execute Subroutine) operation code.

Syntax for Fix Format:

Exsr: It is used to call and process a subroutine.

Factor 1CodeFactor 2ResultResulting indicator
 ExsrSubroutine name  

Begsr: The op-code represents beginning of a subroutine placed in factor-1.

Factor 1CodeFactor 2ResultResulting indicator
Subroutine nameBegsr   

Endsr: ENDSR must be the last statement in the subroutine.

Factor 1CodeFactor 2ResultResulting indicator
Subroutine nameEndsr   

Syntax for Free Format:

Exsr: It is used to call and process a subroutine.

EXSR subroutine-name;

Begsr: The opcode represents beginning of a subroutine.

BEGSR subroutine-name;

Endsr: ENDSR must be the last statement in the subroutine.

ENDSR;

Usage:

Subroutines in RPGLE are used to encapsulate a specific piece of functionality within a program. Here’s how subroutines are typically used in RPGLE:

  • 1. Modularity: Subroutines allow us to divide our RPGLE program into smaller, manageable units of code. Each subroutine can be responsible for a specific task or operation. 
  • 2. Reusability: Once we define a subroutine, we can call it multiple times from within our program, providing code reusability. This reduces code duplication and ensures that changes to a particular functionality only need to be made in one place.
  • 3. Readability: Subroutines make our RPGLE code more readable and understandable by breaking it into smaller, well-named, and well-documented units.
  • 4. Encapsulation: Subroutines can encapsulate complex operations or algorithms, making the main program more focused on the overall flow and logic of the application.

Restriction:

Subroutines can be restricted or limited in various ways based on the programming context and the features of RPGLE itself.

Below are some common restrictions and limitations on subroutines in RPGLE:

  • 1. No Nested Subroutines: RPGLE does not support nested subroutines. This means you cannot define a subroutine within another subroutine. Subroutines are standalone and independent.
  • 2. No Recursion: RPGLE doesn’t directly support recursion within subroutines, which means a subroutine cannot call itself directly or indirectly. Recursive calls are not allowed, as RPGLE does not have the necessary stack management for recursion.
  • 3. No Local Variables: RPGLE subroutines do not have local variables or local storage.
  • 4. Compile-Time Binding: In RPGLE, subroutine calls are typically resolved at compile time rather than at runtime. This means that if you change a subroutine, you often need to recompile all programs that call it.
  • 5. No Explicit Return Statements: RPGLE subroutines do not require explicit “return” statements like some other languages. Control returns automatically to the calling program or procedure at the end of the subroutine.
  • 6. Shared Memory Space: All variables declared within a program, including subroutines, share the same memory space. This means no true local variables exist within subroutines.
  • 7. Propagation of unhandled exceptions: Unhandled exceptions within subroutines propagate to the calling program or higher-level exception handlers.

Best Practices:

  • 1. Design subroutines with clear error handling strategies.
  • 2. Use return codes and indicators effectively for error signalling.
  • 3. Consider external procedures or sub procedures for more structured exception handling.

Example in Fix format:

Here’s a breakdown of the example:

  • a) In first three line declare the variables.
  • b) On 7th line firstly Execute the subroutine with EXSR it is the operation code used to execute the subroutine and then in the factor2 have subroutine name.
  • c) Additionally, we will define the subroutine elsewhere in our program.
  • d) On 8th line start the subroutine process with BEGSR. This is the operation code that marks the beginning of the subroutine.
  • e) From 9th line to 12th subroutine logic code.
  • f) On 13 line is end of the subroutine with ENDSR. This operation code marks the end of the subroutine.

Free format example:

Here’s a breakdown of the example:

  • a) In first three line declare the variable.
  • b) On 6th line firstly Execute the subroutine with EXSR and subroutine name.
  • c) Additionally, we will define the subroutine elsewhere in our program.
  • d) On 8th line start the subroutine process with BEGSR. This is the operation code that marks the beginning of the subroutine.
  • e) From 9th line to 12th subroutine logic code.
  • f) On 13 line is end of the subroutine with ENDSR. This operation code marks the end of the subroutine.

Subroutines in RPG are a way to modularize your code and make it more organized and readable. You can call a subroutine multiple times from different parts of your program, and it allows you to encapsulate and reuse specific logic.

How can we help you?

We have hundreds of highly-qualified, experienced experts working in 70+ technologies.

X

Awards and Certifications

company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo
company-logo