In this section, we will cover the fundamental syntax and structure of RPG (Report Program Generator) programming. Understanding these basics is crucial for writing and reading RPG code effectively.

Key Concepts

  1. Program Structure
  2. Specifications
  3. Comments
  4. Basic Statements

  1. Program Structure

An RPG program is typically divided into several sections, each serving a specific purpose. The main sections are:

  • Control Specifications (H-spec): Define program-wide settings.
  • File Specifications (F-spec): Define files used in the program.
  • Data Definition Specifications (D-spec): Define variables, constants, and data structures.
  • Input Specifications (I-spec): Define input records.
  • Calculation Specifications (C-spec): Contain the logic of the program.
  • Output Specifications (O-spec): Define output records.

  1. Specifications

Control Specifications (H-spec)

Control specifications set global parameters for the program. They are optional but useful for defining program-wide settings.

H dftactgrp(*no) bnddir('QC2LE')

File Specifications (F-spec)

File specifications define the files that the program will use. This includes input, output, and update files.

FMYFILE   IF   E           K DISK

Data Definition Specifications (D-spec)

Data definition specifications are used to define variables, constants, and data structures.

D Name            S             50A
D Age             S              3P 0

Input Specifications (I-spec)

Input specifications define the format of input records. This is less commonly used in modern RPG IV.

I MYFILE
I              1  50  Name
I             51  53  Age

Calculation Specifications (C-spec)

Calculation specifications contain the logic of the program. This is where most of the programming happens.

C     *ENTRY    PLIST
C              PARM           Name
C              PARM           Age
C     Name     DSPLY
C     Age      DSPLY
C     *INLR    SETON

Output Specifications (O-spec)

Output specifications define the format of output records. Like I-specs, these are less commonly used in modern RPG IV.

O MYFILE
O              1  50  Name
O             51  53  Age

  1. Comments

Comments are used to explain the code and are ignored by the compiler. In RPG, comments can be added using // for single-line comments or /* ... */ for multi-line comments.

// This is a single-line comment

/* This is a
   multi-line comment */

  1. Basic Statements

Assignment

Assignment statements are used to assign values to variables.

C     EVAL      Name = 'John Doe'
C     EVAL      Age = 30

Arithmetic Operations

RPG supports basic arithmetic operations like addition, subtraction, multiplication, and division.

C     EVAL      Total = Price * Quantity
C     EVAL      DiscountedPrice = Total - Discount

Conditional Statements

Conditional statements are used to execute code based on certain conditions.

C     IF        Age >= 18
C              EVAL      Status = 'Adult'
C     ELSE
C              EVAL      Status = 'Minor'
C     ENDIF

Loops

Loops are used to execute a block of code multiple times.

C     FOR       i = 1 TO 10
C              EVAL      Sum = Sum + i
C     ENDFOR

Practical Example

Let's put it all together in a simple RPG program that reads a name and age, then displays them.

H dftactgrp(*no) bnddir('QC2LE')

FMYFILE   IF   E           K DISK

D Name            S             50A
D Age             S              3P 0

C     *ENTRY    PLIST
C              PARM           Name
C              PARM           Age

C     Name     DSPLY
C     Age      DSPLY

C     *INLR    SETON

Explanation

  1. H-spec: Sets the default activation group to *no and binds the program to the QC2LE binding directory.
  2. F-spec: Defines MYFILE as an input file.
  3. D-spec: Defines two variables, Name and Age.
  4. C-spec:
    • *ENTRY PLIST and PARM statements define the program's entry parameters.
    • DSPLY statements display the values of Name and Age.
    • *INLR SETON sets the last record indicator to end the program.

Exercises

Exercise 1

Write an RPG program that reads a product name and its price, then displays them.

Solution

H dftactgrp(*no) bnddir('QC2LE')

FMYFILE   IF   E           K DISK

D ProductName     S             50A
D Price           S              7P 2

C     *ENTRY    PLIST
C              PARM           ProductName
C              PARM           Price

C     ProductName DSPLY
C     Price       DSPLY

C     *INLR    SETON

Exercise 2

Modify the previous program to calculate and display the total price for a given quantity.

Solution

H dftactgrp(*no) bnddir('QC2LE')

FMYFILE   IF   E           K DISK

D ProductName     S             50A
D Price           S              7P 2
D Quantity        S              3P 0
D TotalPrice      S              9P 2

C     *ENTRY    PLIST
C              PARM           ProductName
C              PARM           Price
C              PARM           Quantity

C     EVAL      TotalPrice = Price * Quantity

C     ProductName DSPLY
C     Price       DSPLY
C     Quantity    DSPLY
C     TotalPrice  DSPLY

C     *INLR    SETON

Conclusion

In this section, we covered the basic syntax and structure of RPG programming, including the different types of specifications, comments, and basic statements. We also provided practical examples and exercises to reinforce the concepts. Understanding these basics will help you as you progress to more advanced topics in RPG programming.

© Copyright 2024. All rights reserved