Example of an ILE RPG Program

This section illustrates a simple ILE RPG program that performs payroll calculations.

Problem Statement

The payroll department of a small company wants to create a print output that lists employees' pay for that week. Assume there are two disk files, EMPLOYEE and TRANSACT, on the system.

The first file, EMPLOYEE, contains employee records. The figure below shows the format of an employee record:

Figure 1. DDS for Employee physical file
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..*
A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++++*
A          R EMP_REC
A            EMP_NUMBER     5          TEXT('EMPLOYEE NUMBER')
A            EMP_NAME      16          TEXT('EXPLOYEE NAME')
A            EMP_RATE       5  2       TEXT('EXPLOYEE RATE')
A          K EMP_NUMBER

The second file, TRANSACT, tracks the number of hours each employee worked for that week and any bonus that employee may have received. The figure below shows the format of a transaction record:

Figure 2. DDS for TRANSACT physical file
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ..*
A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++++*
A          R TRN_REC
A            TRN_NUMBER     5          TEXT('EMPLOYEE NUMBER')
A            TRN_HOURS      4  1       TEXT('HOURS WORKED')
A            TRN_BONUS      6  2       TEXT('BONUS')

Each employee's pay is calculated by multiplying the "hours" (from the TRANSACT file) and the "rate" (from the EMPLOYEE file) and adding the "bonus" from the TRANSACT file. If more than 40 hours were worked, the employee is paid for for 1.5 times the normal rate.

Control Specifications
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
HKeywords++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
H DATEDIT(*DMY/)

Today's date will be printed in day, month, year format with "/" as the separator.

File Description Specifications
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+...
FFilename++IPEASFRlen+LKlen+AIDevice+.Keywords+++++++++++++++++++++++++++
FTRANSACT  IP   E           K DISK
FEMPLOYEE  IF   E           K DISK
FQSYSPRT   O    F   80        PRINTER
There are three files defined on the file description specifications:
  • The TRANSACT file is defined as the Input Primary file. The ILE RPG program cycle controls the reading of records from this file.
  • The EMPLOYEE file is defined as the Input Full-Procedure file. The reading of records from this file is controlled by operations in the calculation specifications.
  • The QSYSPRT file is defined as the Output Printer file.
Definition Specifications
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+...
D+Name++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++
D Pay             S              8P 2
D Heading1        C                   'NUMBER  NAME              RATE    H-
D                                     OURS  BONUS    PAY       '
D Heading2        C                   '______  ________________  ______  _-
D                                     ____  _______  __________'
D CalcPay         PR             8P 2
D   Rate                         5P 2 VALUE
D   Hours                       10U 0 VALUE
D   Bonus                        5P 2 VALUE

Using the definition specifications, declare a variable called "Pay" to hold an employees' weekly pay and two constants "Heading1" and "Heading2" to aid in the printing of the report headings.

Calculation Specifications
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+...

 /free
     chain trn_number emp_rec;  1 
     *IN99 = NOT %found(employee);  2 
     if %found(employee);  3 
        pay = CalcPay (emp_rate: trn_hours: trn_bonus);
     endif;
 /end-free
The coding entries on the calculation specifications include:
  1. Using the CHAIN operation code, the field TRN_NUMBER from the transaction file is used to find the record with the same employee number in the employee file.
  2. *IN99 is assigned the opposite of %FOUND. A later output specification is conditioned by indicator 99; indicator 99 should have the value *ON if the CHAIN operation did not find a record.
  3. If the CHAIN operation is successful (that is, %FOUND returns *ON), the pay for that employee is evaluated. The result is "rounded" and stored in the variable called Pay.
Output Specifications
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+...
OFilename++DF..N01N02N03Excnam++++B++A++Sb+Sa+...........................
O..............N01N02N03Field+++++++++YB.End++PConstant/editword/DTformat
OQSYSPRT   H    1P                     2  3
O                                           35 'PAYROLL REGISTER'
O                       *DATE         Y     60
O          H    1P                     2
O                                           60 Heading1
O          H    1P                     2
O                                           60 Heading2
O          D   N1PN99                  2
O                       TRN_NUMBER           5
O                       EMP_NAME            24
O                       EMP_RATE      L     33
O                       TRN_HOURS     L     40
O                       TRN_BONUS     L     49
O                       Pay                 60 '$     0.  '
O          D   N1P 99                  2
O                       TRN_NUMBER           5
O                                           35 '** NOT ON EMPLOYEE FILE **'
O          T    LR
O                                           33 'END OF LISTING'
The output specifications describe what fields are to be written on the QSYSPRT output:
  • The Heading Lines that contain the constant string 'PAYROLL REGISTER' as well as headings for the detail information will be printed if indicator 1P is on. Indicator 1P is turned on by the ILE RPG program cycle during the first cycle.
  • The Detail Lines are conditioned by the indicators 1P and 99. Detail Lines are not printed at 1P time. The N99 will only allow the Detail lines to be printed if indicator 99 is off, which indicates that the corresponding employee record has been found. If the indicator 99 is on, then the employee number and the constant string '** NOT ON EMPLOYEE FILE **' will be printed instead.
  • The Total Line contains the constant string 'END OF LISTING'. It will be printed during the last program cycle.

A Subprocedure

The subprocedure calculates the pay for the employee using the parameters passed to it. The resulting value is returned to the caller using the RETURN statement.

The procedure specifications indicate the beginning and end of the procedure. The definition specifications define the return type of the procedure, the parameters to the procedure, and the local variable Overtime.

P CalcPay         B
D CalcPay         PI             8P 2
D   Rate                         5P 2 VALUE
D   Hours                       10U 0 VALUE
D   Bonus                        5P 2 VALUE
D Overtime        S              5P 2 INZ(0)

 /free

    // Determine any overtime hours to be paid.

    if Hours > 40;
      Overtime = (Hours - 40) * Rate * 1.5;
      Hours = 40;
    endif;

    // Calculate the total pay and return it to the caller.

    return  Rate * Hours + Bonus + Overtime;
 /end-free
P CalcPay         E

The Entire Source Program

The following figure combines all the specifications used in this program. This is what you should enter into the source file for this program.

Figure 3. A Sample Payroll Calculation Program
 *------------------------------------------------------------------------*
 * DESCRIPTION:  This program creates a printed output of employee's pay  *
 *               for the week.                                            *
 *------------------------------------------------------------------------*
H DATEDIT(*DMY/)
 *------------------------------------------------------------------------*
 * File Definitions                                                       *
 *------------------------------------------------------------------------*
FTRANSACT  IP   E           K DISK
FEMPLOYEE  IF   E           K DISK
FQSYSPRT   O    F   80        PRINTER
 *------------------------------------------------------------------------*
 * Variable Declarations                                                  *
 *------------------------------------------------------------------------*
D Pay             S              8P 2
 *------------------------------------------------------------------------*
 * Constant Declarations                                                  *
 *------------------------------------------------------------------------*

D Heading1        C                   'NUMBER  NAME              RATE    H-
D                                     OURS  BONUS    PAY       '
D Heading2        C                   '______  ________________  ______  _-
D                                     ____  _______  __________'
 *------------------------------------------------------------------------*
 * Prototype Definition for subprocedure CalcPay                          *
 *------------------------------------------------------------------------*
D CalcPay         PR             8P 2
D   Rate                         5P 2 VALUE
D   Hours                       10U 0 VALUE
D   Bonus                        5P 2 VALUE
 *------------------------------------------------------------------------*
 * For each record in the transaction file (TRANSACT), if the employee    *
 * is found, compute the employee's pay and print the details.            *
 *------------------------------------------------------------------------*

 /free
    chain trn_number emp_rec;
    if %found(emp_rec);
       pay = CalcPay (emp_rate: trn_hours: trn_bonus);
    endif;
 /end-free

 *------------------------------------------------------------------------*
 * Report Layout                                                          *
 *  -- print the heading lines if 1P is on                                *
 *  -- if the record is found (indicator 99 is off) print the payroll     *
 *     details otherwise print an exception record                        *
 *  -- print 'END OF LISTING' when LR is on                               *
 *------------------------------------------------------------------------*
OQSYSPRT   H    1P                     2  3
O                                           35 'PAYROLL REGISTER'
O                       *DATE         Y     60
O          H    1P                     2
O                                           60 Heading1
O          H    1P                     2
O                                           60 Heading2
O          D   N1PN99                  2
O                       TRN_NUMBER           5
O                       EMP_NAME            24
O                       EMP_RATE      L     33
O                       TRN_HOURS     L     40
O                       TRN_BONUS     L     49
O                       Pay                 60 '$     0.  '
O          D   N1P 99                  2
O                       TRN_NUMBER           5
O                                           35 '** NOT ON EMPLOYEE FILE **'
O          T    LR
O                                           33 'END OF LISTING'
 *------------------------------------------------------------------------*
 * Subprocedure  -- calculates overtime pay.                              *
 *------------------------------------------------------------------------*

P CalcPay         B
D CalcPay         PI             8P 2
D   Rate                         5P 2 VALUE
D   Hours                       10U 0 VALUE
D   Bonus                        5P 2 VALUE
D Overtime        S              5P 2 INZ(0)

 /free

    // Determine any overtime hours to be paid.

    if Hours > 40;
      Overtime = (Hours - 40) * Rate * 1.5;
      Hours = 40;
    endif;

    // Calculate the total pay and return it to the caller.

    return(H)  Rate * Hours + Bonus + Overtime;
 /end-free
P CalcPay         E