This is a mirror of a wonderful article from RPG Cafe!
(Short URL: https://ibm.biz/rpgCafe_Freeform_Rpg_Tutorial)
This chapter introduces you to general information about RPG source code.
RPG source code
For free-form statements, the RPG compiler uses only columns 8 - 80 of a source member. Columns 1 -5 and columns 81+ are used for comments. Columns 7 and 8 are only used for fixed form statements. The only fixed-form statements you need to use are I and O statements, and they are rarely used in RPG nowadays.
There are several types of RPG statements.
- Control statements, also called Header statements, also called "H specs" due to the historical fixed-form statements that start with H in column 6. These contain general-purpose keywords that affect the entire module.
- File and Definition statements, also called "F specs" and "D specs". File statements define the files to be used in the module. Definition statements define constants, variables, and prototypes.
- Input specifications, also called "I specs". These define the input record layouts for your files. The RPG compiler generates I specs for externally-described input-capable files.
- Calculation statements, also called "C specs". Here is where you code the logic of your procedures.
- Output specifications, also called "O specs". These define the output record layouts for your files. The RPG compiler generates O specs for externally-described output-capable files.
- Procedure statements, also called "P specs". These start and end subprocedures.
The specifications must appear in the order given above, but it is not necessary to code all the specifications. A module could contain just a single C spec.
There are fixed-form versions of all the statements, but the only ones you might need to use are I and O statements, or a few historical types of File or Calculation statements. This tutorial does not cover fixed-form code. You can see many examples in the ILE RPG Reference.
RPG was originally created to handle files. The intention was that the program would read a record from the "primary file" and for each record, it would perform the calculations. When it reached the last record, it would close the file and end the program.
To artificially create the "last record" situation, the program could set on the "Last Record" indicator, called *INLR.
For programs that do not have a primary file, it is still necessary to stop the program from looping through the calculations. This can be done two ways:
- By using the RETURN operation.
- By setting on the Last Record indicator.
*inlr = '1';
There are subtle differences in these two mechanisms that are discussed in later chapters.