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 2. 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 3. 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:

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;
     if %found(emp_rec);
        pay = CalcPay (emp_rate: trn_hours: trn_bonus);
     endif;
 /end-free

The coding entries on the calculation specifications include:

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:

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 4. 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  Rate * Hours + Bonus + Overtime;
 /end-free
P CalcPay         E


[ Top of Page | Previous Page | Next Page | Contents | Index ]