An Introduction to RPG IV Programming for Beginners
This guide is designed for developers with some programming experience who are eager to dive into RPG IV (often referred to as ILE RPG). It builds on the following prerequisites:
- Access to IBM i version 7.1 or higher, enabling the use of free-form syntax for the majority of RPG IV elements. (Certain examples might require newer versions, which will be highlighted as needed. However, version 7.1 covers most of the material here.)
- No previous exposure to RPG III is necessary.
- Basic familiarity with IBM i (previously known as AS/400, iSeries, or System i).
- Ability to reference the official RPG documentation in the IBM Knowledge Center.
The sample code snippets focus on illustrating specific concepts, but they may include optional "extra highlights" in follow-up notes. These extras point out advanced or upcoming topics. Feel free to skip them initially, they won't impact your understanding of the core content, as everything is explained in detail later.
Chapter 1: Your First Program – "Hello World"
In this section, we'll walk through building and executing a simple RPG IV program from scratch.
It covers two common development environments:
- Modern IDEs like VS Code or Rational Developer for i (RDi)
- Traditional PDM and SEU via a 5250 terminal emulator
For IDE users:
- Establish a connection to your IBM i system.
- Set up a new library and note its name. You'll reference this library when running the program. (Use "yourlibrary" as a placeholder in the steps below.)
- Add a source physical file.
- Create a member named HELLO within that file, with a type of RPGLE.
- Launch the member in your editor.
For SEU users:
- Sign into your IBM i session.
- Create a library using the CRTLIB command.
- Add a source physical file with CRTSRCPF.
- Launch WRKMBRPDM, pointing to your library and source file.
- Add a new member called HELLO (type RPGLE) via F6 in WRKMBRPDM or the ADDPFM command.
- Edit the member using option 2.
- Note: SEU may flag free-form H, F, D, or P specs as errors since its parser hasn't been updated since version 6.1. If you stick with SEU, save and exit despite the warnings.
Writing the Code in Full Free-Form Style
Input the code below. Ensure **FREE starts in column 1 on the first line. IDEs typically display column numbers; in SEU, press F19 to shift left and reveal columns 1-5 (as the default view starts at column 6).
dsply 'Hello World';
return;
Building the Program
For IDEs - Compile directly from the editor menu or by right-clicking the member in the navigator and selecting CRTBNDRPG. Accept all default parameters.
For SEU - Exit with F3, then choose option 14 to compile. Alternatively, run CRTBNDRPG from the command line (prompt with F4).
Running the Program
Enter this command:
This displays objects in the library. It's optional but helpful, as it pauses the job to show the display message. Without it, the program runs silently, check DSPJOBLOG, F10, then F6 for output if needed.
Invoke the program with:
You'll see a display: "DSPLY Hello World". Press Enter to close it and end the program.
Understanding Full Free-Form vs. Column-Constrained Free-Form
RPG supports two free-form variants. The traditional column-constrained style limits code to columns 8 through 80. Full free-form removes these restrictions, allowing code anywhere on the line with unlimited length.
To enable full free-form, place **FREE in column 1 of the first line all subsequent code must be free-form.
Without **FREE (column-constrained mode), you can blend fixed-form (using columns 6-7) and free-form code, but stay within columns 8-80.
This guide uses **FREE for full free-form examples, but the code is compatible with column-constrained mode if you add seven leading spaces and avoid exceeding column 80. With **FREE active, those spaces are optional and can be trimmed.
Handling Compilation Issues
Reopen your source and modify it to display an undefined variable "name":
return;
Remove the prior program version using DLTPGM.
Recompile. and let's assume it fails with a severity 30 error.
In IDEs, the problematic line gets highlighted automatically.
In SEU, run WRKSPLF, F18 to the end, select option 5 on the HELLO spool file, and B to the bottom. Scroll up to errors, noting the highest-severity one (RNF7030). Search for it (F16) to find the statement number (e.g., 2).
Locate that statement in the listing, in this case it's the dsply line!
The issue: "name" lacks a definition. RPG IV treats names case-insensitively ("name" equals "NAME"), but listings use uppercase.
Fix by adding a declaration at the top (keep it between columns 8-80 if needed):
dsply name;
return;
Recompile and run: It now shows "DSPLY Nick".
Using the Debugger
Recompile with a debug view like DBGVIEW(*LIST) (or *ALL, *SOURCE, *COPY all suitable here; avoid *NONE or *STMT). For the system debugger, assuming the program is QTEMP/MYPGM:
F10 to exit setup, or F6 for breakpoints.
Run:
At breakpoints, F10 to step; F11 to inspect variables.
Finish with:
At this point, you should be comfortable with:
- Editing and compiling source code.
- Identifying and resolving compile errors.
- Executing programs.
- Basic debugging techniques.
Future sections will build on these fundamentals.
