Introduction

Free-format RPG is a modernized version of the RPG programming language that allows for more readable and maintainable code. Unlike traditional fixed-format RPG, free-format RPG removes many of the column restrictions, making it easier for programmers to write and understand the code.

Key Concepts

Differences Between Fixed-Format and Free-Format RPG

Feature Fixed-Format RPG Free-Format RPG
Column Restrictions Strict column-based syntax No column restrictions
Code Readability Less readable, more cryptic More readable, similar to other modern languages
Flexibility Limited High
Maintenance Harder to maintain Easier to maintain

Syntax Changes

  • Declaration Statements: In free-format RPG, declaration statements are more flexible and do not require specific columns.
  • Control Structures: Control structures like IF, DO, and SELECT are more intuitive and easier to read.
  • Expressions: Mathematical and logical expressions are written in a more natural and readable way.

Basic Syntax

Variable Declaration

In free-format RPG, variables are declared using the DCL-S keyword.

DCL-S myVariable CHAR(10);
DCL-S myNumber INT(10);

Control Structures

Control structures in free-format RPG are similar to those in other modern programming languages.

IF Statement

IF myNumber > 10;
   // Do something
ELSE;
   // Do something else
ENDIF;

DO Loop

DCL-S i INT(10);

FOR i = 1 TO 10;
   // Loop body
ENDFOR;

Functions and Procedures

Functions and procedures in free-format RPG are defined using the DCL-PROC and END-PROC keywords.

DCL-PROC myProcedure;
   DCL-PI *N;
      myParam CHAR(10);
   END-PI;

   // Procedure body

END-PROC;

Practical Example

Hello World in Free-Format RPG

Let's start with a simple "Hello World" program in free-format RPG.

**FREE
DCL-S message CHAR(50) INZ('Hello, World!');

DSPLY message;

*INLR = *ON;

Explanation

  • **FREE: Indicates the start of free-format code.
  • DCL-S message CHAR(50) INZ('Hello, World!');: Declares a variable message of type CHAR with an initial value of 'Hello, World!'.
  • DSPLY message;: Displays the value of message on the screen.
  • *INLR = *ON;: Sets the last record indicator to ON, signaling the end of the program.

Exercises

Exercise 1: Simple Arithmetic

Write a free-format RPG program that calculates the sum of two numbers and displays the result.

Solution

**FREE
DCL-S num1 INT(10) INZ(5);
DCL-S num2 INT(10) INZ(10);
DCL-S sum INT(10);

sum = num1 + num2;

DSPLY sum;

*INLR = *ON;

Exercise 2: Conditional Logic

Write a free-format RPG program that checks if a number is even or odd and displays the result.

Solution

**FREE
DCL-S num INT(10) INZ(7);
DCL-S result CHAR(10);

IF (num % 2 = 0);
   result = 'Even';
ELSE;
   result = 'Odd';
ENDIF;

DSPLY result;

*INLR = *ON;

Common Mistakes and Tips

  • Indentation: While free-format RPG is more flexible, maintaining proper indentation is crucial for readability.
  • Variable Initialization: Always initialize variables to avoid unexpected behavior.
  • Control Structures: Ensure that control structures are properly closed with their respective ENDIF, ENDFOR, etc.

Conclusion

Free-format RPG offers a more modern and flexible approach to RPG programming, making it easier to write, read, and maintain code. By understanding the basic syntax and structure, you can leverage the power of free-format RPG to create efficient and maintainable programs. In the next topic, we will delve into ILE concepts, which further enhance the capabilities of RPG programming.

© Copyright 2024. All rights reserved