LESSON 4 · Intermediate

RPG development: from opcodes to procedures

Plain-English notes, safe code skeletons, IBM i commands, and a checkpoint you can use before opening the deeper question bank.

After this lesson

Learning notes

Read RPG in the order the compiler sees it

Begin with control options and file or data declarations. Then identify entry parameters, procedures, and the main business operation. Finally trace I/O and status checks. This order is more reliable than starting at the first calculation line and guessing what every field means.

RPG opcodes express intent such as CHAIN, SETLL, READE, UPDATE, CALL, and EXSR. Built-in functions such as %FOUND, %EOF, %TRIM, and %SUBST report state or transform values. Say what state each operation changes before explaining syntax.

  1. Declare files, prototypes, and data
  2. Receive parameters and validate input
  3. Read or query the required records
  4. Apply business rules in a procedure
  5. Write results and return a clear status

Fixed format and fully free format carry the same intent

Fixed-format RPG is common in maintenance code because its columns carry meaning. Fully free RPG makes declarations and operations easier to review in new work. The important interview skill is preserving behavior while making status handling and data scope clearer.

Keep examples small enough to test. A keyed read should show the found check, and a write should show the validation and error path.

One behavior, two source styles

Fixed format

C     orderId       CHAIN     OrderRec
C                   IF        %FOUND(Orders)
C                   EVAL      status = 'READY'
C                   UPDATE    OrderRec
C                   ENDIF

Fully free

**FREE
chain orderId OrderRec;
if %found(Orders);
  status = 'READY';
  update OrderRec;
endif;

Use procedures as contracts

A procedure should have a clear purpose, typed parameters, and a predictable result. Put conversion, validation, and database access behind small procedures so a test can exercise them without running an entire display program.

For subfiles and display files, keep screen flow separate from data retrieval. The UI procedure can request a page of rows; a data procedure can own the key range and status rules.

Procedure skeleton

dcl-pr buildGreeting varchar(80);
  customerId packed(9:0) const;
end-pr;

dcl-proc buildGreeting;
  dcl-pi *n varchar(80);
    customerId packed(9:0) const;
  end-pi;
  // validate, read, and return one explicit result
end-proc;

Open the detailed question chapters

Practice checkpoint

Answer all five, then review the explanations. The interactive site stores your completed learning checkpoints in this browser.

  1. 1. What does SQLRPGLE commonly identify?
    1. A job description
    2. Only fixed-format RPG II
    3. A database schema
    4. RPG source with embedded SQL
  2. 2. Which type choice is usually suitable for fixed-scale money?
    1. Packed decimal with explicit precision and scale
    2. An arbitrary text buffer
    3. A job number
    4. A pointer
  3. 3. Does free-form conversion automatically remove global state?
    1. Yes, always
    2. No
    3. Only on Power hardware
    4. Only in QTEMP
  4. 4. Which BIF checks whether CHAIN found a record?
    1. %TRIM
    2. %CHAR
    3. %FOUND
    4. %LEN
  5. 5. A program fails only on its second call in one job. Investigate:
    1. Only source indentation
    2. Only the terminal emulator
    3. Whether the file has a long name
    4. Retained state and cleanup
Show answer key and explanations

1. D — RPG source with embedded SQL It signals the SQL-aware RPG build path.

2. A — Packed decimal with explicit precision and scale Decimal arithmetic supports an explicit business rounding and range policy.

3. B — No Source layout and program architecture are separate concerns.

4. C — %FOUND Check the relevant file status immediately after the read.

5. D — Retained state and cleanup RETURN and runtime lifecycles can preserve state across calls.

IBM documentation for this path