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:
- Recognize the layout rules of RPG source members
- Identify the different RPG specification types (H, F, D, I, C, O, P)
- 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 Type | Description |
|---|---|
| H Specs | Control/Header statements. Set module-wide attributes. |
| F Specs | File declarations. Define files used in the program. |
| D Specs | Definitions. Declare constants, variables, prototypes. |
| I Specs | Input specs. Define input record layouts (compiler-generated for externally-described files). |
| C Specs | Calculation specs. Contain the logic and operations of your program. |
| O Specs | Output specs. Define output record layouts (compiler-generated). |
| P Specs | Procedure 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:
- Read a record
- Perform calculations
- Repeat until end-of-file
- Close the file and end the program
To simulate the end of processing, RPG uses the Last Record Indicator: *INLR
For programs without a primary file, you still need to signal the end of processing. You can do this in two ways:
- Use the
RETURNoperation - 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.
