In this section, we will write our first RPG program: the classic "Hello World" example. This simple program will help you understand the basic structure and syntax of RPG.

Key Concepts

  1. Program Structure: Understanding the basic layout of an RPG program.
  2. Source Specifications: Learning about the different specification types in RPG.
  3. Output Operations: How to display output to the user.

Program Structure

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

  • Control Specifications: Define the overall behavior of the program.
  • File Specifications: Define the files used by the program.
  • Data Description Specifications: Define the data structures and variables.
  • Calculation Specifications: Contain the logic of the program.
  • Output Specifications: Define the output format.

Source Specifications

RPG uses different specification types, each identified by a letter in the first column of the source line:

  • H: Control Specifications
  • F: File Specifications
  • D: Data Description Specifications
  • C: Calculation Specifications
  • O: Output Specifications

For our "Hello World" program, we will primarily focus on the H, D, and C specifications.

Writing the "Hello World" Program

Let's write a simple RPG program that prints "Hello World" to the console.

Step-by-Step Code Explanation

  1. Control Specifications (H-spec): Define the program's overall behavior.
  2. Data Description Specifications (D-spec): Define the variables.
  3. Calculation Specifications (C-spec): Contain the program logic.

Code Example

H DFTACTGRP(*NO) ACTGRP(*NEW)

D msg             S             50A   INZ('Hello World')

C                   EVAL      *INLR = *ON
C                   DSPLY                   msg

Detailed Explanation

  1. Control Specifications (H-spec):

    H DFTACTGRP(*NO) ACTGRP(*NEW)
    
    • DFTACTGRP(*NO): Indicates that the program does not run in the default activation group.
    • ACTGRP(*NEW): Specifies that the program runs in a new activation group.
  2. Data Description Specifications (D-spec):

    D msg             S             50A   INZ('Hello World')
    
    • msg: A variable of type character (A) with a length of 50.
    • INZ('Hello World'): Initializes the variable msg with the string "Hello World".
  3. Calculation Specifications (C-spec):

    C                   EVAL      *INLR = *ON
    C                   DSPLY                   msg
    
    • EVAL *INLR = *ON: Sets the last record indicator (*INLR) to *ON, which ends the program.
    • DSPLY msg: Displays the value of msg to the console.

Practical Exercise

Exercise

Write an RPG program that displays the message "Welcome to RPG Programming!" on the console.

Solution

H DFTACTGRP(*NO) ACTGRP(*NEW)

D welcomeMsg      S             50A   INZ('Welcome to RPG Programming!')

C                   EVAL      *INLR = *ON
C                   DSPLY                   welcomeMsg

Explanation

  • The welcomeMsg variable is initialized with the string "Welcome to RPG Programming!".
  • The DSPLY operation displays the value of welcomeMsg to the console.

Common Mistakes and Tips

  • Forgetting to set *INLR to *ON: This will cause the program to not terminate properly.
  • Incorrect initialization of variables: Ensure that the variable type and length match the intended use.
  • Misplacing specifications: Ensure that each specification type is placed in the correct section of the program.

Conclusion

In this section, you learned how to write a simple "Hello World" program in RPG. You now understand the basic structure of an RPG program, including control, data description, and calculation specifications. This foundational knowledge will be crucial as you progress through more complex topics in RPG programming.

© Copyright 2024. All rights reserved