Free-Format RPG IV: How to Bring Your RPG Programs Into the 21st Century

Free-Format RPG IV: How to Bring Your RPG Programs Into the 21st Century

by Jim Martin

NOOK Book(eBook)

$34.99 $59.99 Save 42% Current price is $34.99, Original price is $59.99. You Save 42%.
View All Available Formats & Editions

Available on Compatible NOOK Devices and the free NOOK Apps.
WANT A NOOK?  Explore Now
LEND ME® See Details


What could be more important than upgrading your RPG skills? Traditional fixed-format style RPG IV programmers everywhere are embracing the increased productivity, better readability, and easier program maintenance that comes with the free-format style of programming in RPG IV. This book, a "how-to" guide and the first of its kind, will provide you with concise and thorough information on this powerful new way of coding RPG IV. Though free-format information is available in IBM manuals, it is not separated from everything else, thereby requiring hours of tedious research to track down the information you need. With this book, your search is over. Free-Format RPG IV is the "one-stop" resource that will save you time and frustration. In it, you'll find everything you need to know to write RPG IV in the free-format style. Author Jim Martin not only teaches rules and syntax, but also explains how this new style of coding has the potential to improve overall programmer productivity—from initial development through maintenance. He methodically covers the "how to" involved in free-format RPG IV, including many style issues, such as named indicators and built-in functions. He also addresses input and output, program flow, data manipulation, and math operations. There is even a chapter on prototyping and APIs. Free-Format RPG IV is the definitive guide on the topic and just what you need to get started with this exciting, powerful style of programming. With Free-Format RPG IV you will: - Learn not only the "how-to," but also the "why" of free-format RPG IV - Acquire essential, free-format style tips to help you write good, maintainable code - Find complete-program solutions for specific problems, including associated display and printer files - Discover how upgrading your RPG skills to free-format can take your programming career to the next level.

Product Details

ISBN-13: 9781583476246
Publisher: Mc Press
Publication date: 03/01/2012
Sold by: Barnes & Noble
Format: NOOK Book
Pages: 216
File size: 4 MB

About the Author

Jim Martin has written RPG applications for IBM and has worked as a programmer of portions of CPF and OS/400. He is an IBM certified RPG IV developer. He lives in Clever, Missouri.

Read an Excerpt

Free-Format RPG IV

By Jim Martin

MC Press

Copyright © 2009 Jim Martin
All rights reserved.
ISBN: 978-1-58347-624-6


Pre–Free-Format RPG IV

The RPG programming language has certainly come a long way since its introduction as a simple report program generator in the 1960s. From the earliest versions of RPG to the latest release of free-format RPG IV on IBM's eServer iSeries (now i5) platform, the changes have been dramatic.

The centerpiece of today's RPG is its support for a free-format style of calculation coding. Free format puts RPG on the map of modern programming languages and gives programmers important new functionality for constructing programs that are easier to create, understand, and maintain. If you're writing RPG programs today, you owe it to yourself to make the most of this great new function.

In this book, you'll learn the ins and outs of free-format RPG, from I/O and program flow to data manipulation, math operations, and call and return. But before we dive into the delights of free-format RPG, let's take a brief look at its precursor: fixed-format RPG IV.


Even before free format, RPG IV was a powerful, functionally rich programming language. RPG IV — or ILE RPG, as it's officially known — debuted in late 1994 with Version 3 Release 1 (V3R1) of the OS/400 operating system. IBM had introduced the Integrated Language Environment (ILE) in a prior release, but everyone eagerly awaited RPG's entrée into ILE. Three major additions to RPG in V3R1 brought the language a significant level of sophistication that was previously absent: the extended Factor 2, built-in functions, and subprocedures.

Extended Factor 2

The introduction of the extended Factor 2 format, along with operations to exploit it, represented a huge departure from RPG's past. With this enhancement to calculation specifications, RPG programmers could begin to use operations such as Eval (Evaluate expression) and If (If) with long expressions.

The ability to use math symbols (+, -, *, /) within expressions was a major step in modernizing the language. Other symbols, used in comparison expressions, included equal (=), less than (<), greater than (>), less than or equal (<=), greater than or equal (>=), and not equal (<>). The exponentiation symbol (**) became the newest math operator, and support for parentheses let programmers indicate higher precedence in math or comparison expressions. Other programming languages had used these symbols for a while, and IBM chose to bring them into its new version of RPG.

One of the symbols, the plus sign (+), found an additional use in RPG IV: concatenation. Using this new operator, RPG programmers could put character data together into strings much more easily than with previous methods.

Built-in Functions

iSeries programmers were already familiar with built-in functions through their use of such functions in CL, OS/400's control language. The %Sst (substring) and %Bin (binary) functions, for example, are commonly used CL built-in functions.

The whole idea of an RPG IV built-in function was based on the C language's concept of a function. A built-in function performs one task, with or without parameters, and usually returns data of a predetermined data type to the point in the program where the function was specified. For example, RPG IV's %Eof built-in function checks for end-of-file after a Read operation, eliminating the need to code a resulting indicator on the Read. The %Found built-in function lets you check for a record-found condition after a Chain operation without using a resulting indicator.

The number of built-in functions available in RPG has grown from the dozen or so provided in RPG IV's initial release to 76 as of V5R3. Many of these "built-ins" duplicate the function of older operation codes, such as Scan (built-in function %Scan) and Subst (built-in function %Subst). In some cases, the built-in function provides more capabilities than the operation code it replaces. For example, the %Subst built-in function can substring a target as well as a source string, whereas the Subst (Substring) operation code can substring only a source string.


As if these new capabilities weren't enough, IBM added another powerful feature to RPG IV. Support for subprocedures gives RPG IV programmers the option to break complex programs into smaller parts and to create "homegrown" built-in functions if desired. For the first time in RPG, IBM introduced the concept of a local variable for use in subprocedures. Also for the first time, a routine — in this case, a subprocedure — could use recursion.

Other Changes

In RPG IV, IBM also eliminated many of the old constraints of RPG/400 (RPG IV's predecessor). Eight-character file and record names were lengthened to 10. Field name lengths were increased, too — at first to 10 and then to 4,096! The maximum size of character fields grew from 256 bytes to 32,767. Packed-numeric field size was raised to 30 and then 31 digits; it now stands at 63.

Even fans of binary and hexadecimal programming got something new in RPG IV. Previously, you could convert two-byte binary numbers to four-digit decimal numbers and convert four-byte binary numbers to nine digits. The new RPG IV language provided an integer data type, both signed and unsigned, up to 20 digits. To handle ultra-small and ultra-large numbers, the floating point data type made its debut, in both standard and double precision.

Other data types added to the language include the pointer data type, which contains the address of data or a procedure; the object data type, used to handle references to a Java object; and the date, time, and timestamp data types.

Adding these new data types and enlarging the older ones must have come from some greater motivating factor. Why did IBM provide all these things? In my opinion, the reasons lay in maintaining the iSeries as a modern and viable solution for the midrange server marketplace. Older RPG can easily be called a "dinosaur" in this modern age. The gap in programming capabilities between older RPG and modern languages such as C, C++, Java, and others was huge. With today's RPG IV, there is no gap. RPG IV has "come from behind" to be a major player in the modern world of application development.

Some "Baggage" That Came Along

Like any responsible vendor, IBM doesn't like to offend any of its customers. If you wrote an RPG program in 1969 that used the RPG cycle and 99 indicators, IBM won't tell you today that your approach is obsolete. Instead, IBM continues to provide a compatibility path from the "here and now" back to almost any previous time in RPG's history. The machines may change every six to eight years, but programs and the inherent investment made in developing them are preserved. One byproduct of this commitment to long-time customers and their legacy code is that RPG IV, in its current form, carries the weight of 40 years of "baggage." The RPG cycle, and its emulation of the gray accounting machines of the 1960s, lives on.

When IBM introduced RPG IV, many of us hoped we might see a break with the past. Companies that cared little about change could stay with RPG/400, and those interested in new ideas could embrace the new incarnation of RPG. Maybe, I thought, the new RPG would at least drop the cycle. Perhaps we'd only be able to use newer instructions such as Eval, while oldies such as ADD, SUB, MULT, and DIV would be relegated to the history books. Maybe calculation "definitions" would be eliminated in light of the new definition specifications. But no, we got all the older operations and functions along with the new. Today's RPG IV truly is a huge collage of language functionality, encompassing both old and new functions!

New programs don't have to use the old operation codes, primary files, or variables defined in calculations. All this is true. But did you know that a kernel of the RPG cycle code is a part of every RPG IV program, even if you don't specify a primary file? How else could the LR and RT indicators do their jobs?

The Future

In the next chapter, I introduce you to free-format RPG IV. Subsequent chapters delve into the details of this new style of coding and provide plenty of examples to demonstrate the features of the language. Here's a preview of some of the good news: In a major break with the past, most of the original-format operations aren't available in free-format RPG.

Modern programmers see the free-format style as RPG's newfound strength — an enhancement that brings RPG up to the standard set by contemporary programming languages. There will always be some who will criticize IBM for omitting a preferred language function in free format. Regardless, free-format RPG IV is a great, modern programming language, and it's growing!


Introducing Free-Format RPG IV

RPG programmers gained the ability to create free-format calculations in RPG IV with the arrival of V5R1 of WebSphere Development Studio, IBM's application development tool suite for the iSeries system. Free format's arrival caused little fanfare, and most RPG IV developers with whom I spoke at the time weren't very interested in the concept.

A small percentage of my RPG IV programmer friends tried to use free format and asked me for help in their work. The ambitious ones who gave it a try found themselves pleased with the results. However, due to limitations in the first release of free format, RPG calculations still required many lines of fixed-format code, such as for Klist (Define a composite key) and Kfld (Define parts of a key) operations. Mixing fixed- and free-format calculations made programs look "clunky" and certainly didn't help convince co-workers to convert to the new style.

In V5R2, IBM provided additional functionality that let RPG IV programmers use key arguments within a free-format Chain or Set xx operation or use the %Kds built-in function with a data structure that uses the *Key keyword. These enhancements represented a big step forward in "de-clunking" the calculations. Other new functions in V5R2, such as the += accumulative assignment operator (which adds the result of an expression to the target of the assignment), gave free-format RPG procedures a C- and Java-like appearance. Additional built-in functions at that time also helped modernize RPG IV's procedures.

Today, free-format calculations in RPG IV can look like any other modern programming language. Newcomers to RPG IV programming who have prior experience in C, COBOL, PL/1, or another free-format language find free-format RPG IV simple to learn and use. Now that free-format calculations have gained a modicum of acceptance, many fixed-format RPG IV "old-timers" are taking a good look at the new free-format style.

For those new to free format, getting started is sometimes the toughest part. Frankly, it's not easy to change the way you've done something for a long time, especially programming. If you've come this far and want to try writing a little program in the new format, this book will help you begin. We start our tour of free-format RPG IV in this chapter, with an overview of the free-format structure and a look at some key operations and features.

The Free-Format Code Block

The first step in writing free-format calculations is to tell the RPG IV compiler you're going to do so. You begin a block, or section, of free-format RPG by first entering the compiler directive /Free, placing the slash (/) in position 7 and leaving the rest of the source line (positions 12–80) blank. (Free-format RPG IV isn't case sensitive, so the compiler views /Free, /free, and /FREE as the same instruction.) A corresponding /End-free compiler directive, coded on a line by itself following the calculations, signals the end of your free-form block of code. If you forget to include the /End-free in your source program and there are no other calculations, the compiler will not flag the omission as an error.

Between the /Free and /End-free directives, you enter your free-format RPG IV code. Calculations can begin in position 8 or beyond within the free-format code block. Positions 1–5 are available for anything (e.g., change control information). Position 6 must be blank, and position 7 is reserved for additional compiler directives. Free-format calculations end in position 80.

Inside the free-format code block, all supported free-format operations, as well as all built-in functions, are available to you. In contrast to its fixed-format older brother, which has 112 operations, free-format RPG IV provides just 55 free-format operations (as of V5R3). However, free-format RPG IV isn't simply a "stripped-down" subset of its fixed-format counterpart. IBM has created many built-in functions to provide functionality equivalent to (or better than) most of the "missing" operation codes. As of V5R3, RPG IV provides a total of 76 built-in functions.

Coding Free-Format Operations

A line of free-format source begins with a free-format operation code, followed by one or more spaces, the Factor 1 operand, one or more additional spaces, and then the Factor 2 operand. Free-format RPG IV has no result field operand. Instead, you perform arithmetic and character-management operations using assignment statements.

An implication of no result field is the inability to define work variables "on the fly" as RPG programmers commonly did years ago. However, thanks to the powerful nature of free-format expressions, we don't need as many work fields today, and defining them all in definition specifications makes program maintenance easier and more productive.

The final requirement for a line of free-format RPG IV source code is a terminating semicolon (;), followed by a comment if desired. You enter a comment by keying two slashes (//) followed by the text of the comment. You can also place a comment on a line by itself.

Listing 2-1 shows a sample block of free-format code, including a few free-format operations and some comments.


Naming Variables

Free-format RPG IV's rules for naming variables are no different from fixed format's, but when employing longer names (more than 14 characters), you must use either the extended Factor 2 format or free format in your calculations. Variable names must begin with a character and can be in any case. The character can be any one of the 26 regular alphabet characters or the special character #, $, @, or _. Numbers (0–9) are optional and can be used after the first character. Variable names cannot contain blanks, but you can use the underscore character (_) as a word separator to form a multiword name (e.g., Miles_per_gallon).

Until RPG IV came along in late 1994, RPG variable names were limited to six characters. This limitation included references to arrays and their indexes. The first version of RPG IV supported 10-character names, matching the size maximum in DDS for variable naming. A few years ago, IBM extended the variable name length to its present limit of 4,096! Not many programmers are interested in using such long names, but it's sure nice not to be constrained either.

Free-format RPG has a "semi" restriction for variable naming. In free-format, the Eval operation code may be dropped if no op-code extenders are needed. However, if a variable name is the same spelling as an operation code such as In, Out, Select, and others, you must specify the Eval operation code when the variable is used on the left side of the assignment. I suggest not using variables named the same as operation codes, eliminating confusion and restrictions.


You code mathematical expressions in free format using the same methods as fixed format's extended Factor 2. The standard math functions of add, subtract, multiply, and divide are performed within expressions using the +, -, *, and / operators, respectively. IBM also introduced the exponentiation operator (**) in RPG IV, permitting an expression to use a fraction, whole number, or mixed number as an exponent. This operator adds a powerful new capability to the language that was previously unavailable without calling a C program. The free-format option gives you more room on a line for complex mathematical expressions.

Character Strings

You construct character-string expressions in free format just as you do for fixed format's extended Factor 2. Many built-in functions pertain to character strings or converting numeric information to character.

Programming Style

No other rules apply when creating source statements in free-format calculations. However, good programming style should prompt us to enter statements in an ordered way that makes a program's logic easier to understand during program maintenance. Good style would dictate entering a program's "outer" logic as far left as possible (position 8) and beginning "inner" logic two spaces to the right. Continue this indenting process until you're about halfway across the page. If you need deeper groups, you'll have to decide whether to continue to the right or start over again at position 8.


Excerpted from Free-Format RPG IV by Jim Martin. Copyright © 2009 Jim Martin. Excerpted by permission of MC Press.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Table of Contents


Title Page,
Copyright Page,
1 - Pre–Free-Format RPG IV,
2 - Introducing Free-Format RPG IV,
3 - Input/Output Using Free Format,
4 - Program Flow Using Free Format,
5 - Data-Manipulation Operations Using Free Format,
6 - Math Operations in Free Format,
7 - Call and Return in Free Format,
8 - Solutions for Problem Situations,
9 - The Case for Free-Format RPG,
10 - Sample Programs,
A - Free-Format Operations,
B - Additional Elements Used by the Sample Programs,
C - Free-Format Alternatives for Fixed-Format Operations,

Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews

Free-Format RPG IV: How to Bring Your RPG Programs Into the 21st Century 0 out of 5 based on 0 ratings. 0 reviews.