Getting Started with IBM i RPG: RPGLE Overview

Understanding RPG Source Code: Structure, Specs & the RPG Cycle

Before diving into procedures and file handling, it's essential to understand how RPG source code is structured. This lesson introduces the anatomy of an RPG module, the types of statements you'll encounter, and the legacy RPG cycle that still influences modern development.

By the end of this lesson, you’ll be able to:

  1.  Recognize the layout rules of RPG source members
  2. Identify the different RPG specification types (H, F, D, I, C, O, P)
  3. Understand the purpose of the RPG cycle and how to control program flow

RPG Source Member Layout

RPGLE source members follow a specific column-based format:

  • Columns 1–5: Reserved for comments or sequence numbers
  • Column 6: Used for fixed-form spec identifiers (e.g., H, F, D)
  • Columns 7–8: Used for fixed-form continuation or indicators
  • Columns 8–80: Where free-form RPG code lives
  • Columns 81+: Ignored by the compiler (often used for comments)

In modern RPGLE, you’ll mostly use free-form syntax, which lives comfortably between columns 8–80. Fixed-form is largely obsolete, except for rare cases involving I and O specs.

RPG Specification Types

RPG source code is organized into specifications, each serving a distinct purpose. Here’s a breakdown:

Spec TypeDescription
H SpecsControl/Header statements. Set module-wide attributes.
F SpecsFile declarations. Define files used in the program.
D SpecsDefinitions. Declare constants, variables, prototypes.
I SpecsInput specs. Define input record layouts (compiler-generated for externally-described files).
C SpecsCalculation specs. Contain the logic and operations of your program.
O SpecsOutput specs. Define output record layouts (compiler-generated).
P SpecsProcedure specs. Mark the beginning and end of subprocedures.

Note: Specs must appear in the order above, but you don’t need to use all of them. A minimal RPG module might contain just a single C spec.

Fixed vs Free-Form RPG

While RPG historically relied on fixed-form syntax, modern RPGLE favors free-form for clarity and maintainability. This course focuses exclusively on free-form syntax except for occasional mentions of I and O specs, which still use fixed-form in some scenarios.

The RPG Cycle: A Legacy Worth Knowing

RPG was originally designed to process records from a primary file in a loop:

  1.  Read a record
  2. Perform calculations
  3. Repeat until end-of-file
  4. Close the file and end the program

To simulate the end of processing, RPG uses the Last Record Indicator: *INLR

*inlr = '1';

For programs without a primary file, you still need to signal the end of processing. You can do this in two ways:

  •  Use the RETURN operation
  • Set *INLR = '1'

These two approaches behave slightly differently details you'll explore in later lessons.

Summary

Understanding RPG source structure and the RPG cycle sets the foundation for everything that follows. With free-form syntax and modular specs, RPGLE is more readable and powerful than ever. Now that you know how the pieces fit together, you're ready to start building real-world logic.

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