Seriously what is RPG? As a beginner programmer new to development but intermittent at gamming I automatically thought the acronym stood for ‘Role Playing Game,’ however a quick Google search told me I was wrong. RPG stands for Report Program Generator and is a high-level programming language that serves a wide array of business applications and uses. Leveraging chapter 2 of the book Programming in ILE RPG, Fifth Edition by Bryan Meyers, and Jim Buck I will be explaining and summarizing examples and characteristics of the RPG language in order to understand it better.

ILE RPG or the Integrated Language Environment RPG is an implementation of the RPG 4 programming language, it is one of the families of ILE compilers available on IBM® i. Simplified it’s a programing language like Javascript or Python only much older, it first appeared in 1959! In order to understand the basics of RPG the three main sections must be considered;

  1. Control options section
  2. Declarations section
  3. Main procedures section

Although not every RPG program will include all three of these sections they must appear in this order within the source code. Some notes about RPG before diving in; the language isn’t case sensitive so upper or lowercase may be used however, it is best practice to use a title case where each word in the source code is capitalized. Columns 6–7 must be left blank or else the compiler will assume that the line uses a fixed format which is problematic if using free code or even switching between the two so it’s best to leave them blank! Lastly, to add a comment into RPG code begin with two backslashes ‘//’ followed by whatever text needs to be added, if you are familiar with JavaScript this is the same method.

Ctl-opt or Control options are used for controlling the program’s behavior and specifying certain compiler options. It is used for default RPG formats (eg. date formats), changes to the normal processing modes (e.g., changing the internal method the program uses to evaluate expressions), special options to use when compiling the program, and language enhancements that affect the entire program. If using control options add them to the first statement of the program. Also, the program is allowed to have multiple control options.

Declarations are used to define all the data items that the program needs to do its job, the following is an example of two types of declarations: file declaration and standalone declaration.

File declarations are necessary when adding each and every file to an RPG program, to do so begin with a Dcl-f (declare file) instruction, it's customary for input files to be declared first so that all record formats and fields become available for the program.

Once Dcl-f has been created add the file name. Filenames can be a maximum of 10 characters, they must begin with an alphabetic character or one of the special characters $, #, or @ and have no blank or embedded spaces. In the example above the input filename is Customers on the first line and the report filename is Custlist. In summary, the word following Dcl-f is the filename. After successfully declaring the file and naming it's time to add the Device. The entry following the filename indicates the device associated with a file, in the example above the device is Disk. Continuing with the creation of a file declaration the two types of File Usage need to be explained, this is how the program is going to use the file. The first file usage is input files, this contains data to be read by the program, the second is an output file this is the destination for writing output results from the program. Before moving on I would like to point out that RPG has MANY keywords but only those that appear in the example will be examined as a way of not overwhelming this article. Oflind (Overflow indicator) is the name given to the condition that happens when a printed report reaches the bottom of a page, the oflind keyword associates the printer device with the overflow indicator for that file. In the example, the overflow indicator is named Endofpage (this will be explained more later on).

The next type of declarations are Standalone Variable Declarations like their name insinuates, variable declarations describe those variables that do not originate from a file and that do not depend upon a specific structure or format. For these data items, the program needs to know, at a minimum, the name of the variable and its data type (e.g., character or numeric). For example, the below line of code declares one variable by using the Dcl-s (Declare Standalone Variable) instruction.

Variable Name within the variable declaration is similar to the file name, that is the first entry following the Dcl-s instruction names the variable and it must begin with an alphabetic character or one of the special characters $, #, or @. Data types are another important aspect of RPG, one data type is the Indicator, which many other computer languages refer to as a Boolean data type. In the example, Endofpage is an indicator that the program uses to signal printer overflow, Endofpage is then initialized with the value *on. An additional keyword is Inz (Initialize) and the purpose of a variable is to hold a value, once a standalone variable has been defined, it can be assigned value, used within an operation, or printed. To conclude, the Inz keyword supplies an initial value for a variable.

Moving on to the Main Procedure Section, the main procedure is the first part of your program that is executed when you initially call it. When the program is initially called the RPG cycle begins, signaling storage and executing the main procedures and the Cycle Main Program, this function is responsible for starting up and shutting down the program. Everything needs to work harmoniously or else one bug will throw a wrench into the functionality of the entire program, that's why it's important to pseudocode out all of the logic before even starting to write the RPG code. The example program below is a simple read/write program in which you want to read a record, increment the record count, process that record, and repeat the process until no more records exist in the file (a condition called end-of-file). This kind of application is termed Batch Processing because once the program begins, a batch of data (accumulated in a file) directs its execution.

The above code is made up of RPG operations such as Read, Dow, Enddo, If, Endif, Write, Eval, and Return.

  1. Read (Read Sequentially), Define as Usage(*Input), an input operation that instructs the computer to retrieve the next sequential record from the named input file — in this case, your Customers file
  2. Dow (Do While), Enddo, the Dow operation establishes a loop in RPG. An Enddo operation signals the end of the loop. Every Dow operation requires a corresponding Enddo operation to close the loop.
  3. If, If the ‘If’ operation is true, all the calculations between the If and its associated Endif operation are executed. Endif operation marks the end of an If operation’s scope.
  4. Write (Write a Record) directs the program to output a record to an output file.
  5. Eval (Evaluate Expression) assigns a value to a variable, for example; Eval Endofpage = *Off assigns the value *Off to the overflow indicator Endofpage
  6. Return (Return to Caller) returns control to the program that called it — either the computer’s operating system or perhaps another program. Return clearly signals the endpoint of your program and lets the program become part of an application system of called programs.

The last aspect of RPG I’ll be discussing is Free Code and Fixed Format Specifications, like both names hint free code can be added in no particular order, however, fixed-format specifications must appear in a specific order, or sequence, within your source code. The order is as follows;

  • Header (Control) specifications — provide default options for the source
  • File specifications — identify the files a program is to use
  • Definition specifications — define variables and other data items the program is to use
  • Input specifications — depict the record layout for program-described input files 40 Programming in ILE RPG
  • Calculation specifications — detail the procedure the program is to perform
  • Output specifications — describe the program output (results)
  • Procedure boundary specifications — segment the source into units of work, called procedures

If you have made it to the end of this article thank you! Please let me know if anything I have added is incorrect or misleading regarding the ILE RPG language, I will appreciate any feedback and commentary given.

new to code!