Introducing Free-Format RPG IV, Part 1

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

For those new to free format, getting started is sometimes the toughest part.

By Jim Martin

Editor's Note: This article is excerpted from chapter 4 of Free-Format RPG IV: Third Edition, by Jim Martin.

Frankly, it’s not easy to change the way you’ve done something for a long time, especially programming. If you have 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 calculations in this chapter, with an overview of the free-format structure and a look at some key operations and features.

Coding Free-Format Calculations

The first step in writing free-format calculations is remembering to place your free-format RPG IV code in position 8 or beyond. Positions 1–5 are available for anything (e.g., change control information). Position 6 must be remain blank, and position 7 is reserved for compiler directives, such as /SQL. Free-format calculations end in position 80.

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 153 operations, free-format RPG IV provides just 62 free-format operations (as of V7.1). 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 V7.1, RPG IV provides a total of 80 built-in functions.

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 (Eval operations without the Eval).

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 declaring them in definition specifications makes program maintenance easier and more productive.

The final requirement for a line of free-format RPG IV source 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 comments on a line by themselves.

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

Introducing Free-Format RPG IV, Part 1 - Figure 1 

Listing 4-1: Sample free-format block

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 uses 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 that are named the same as operation codes, to eliminate confusion and restrictions.

Programming Style

No other rules apply when entering 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 will have to decide whether to continue to the right or to start over again at position 8.

Listing 4-2 shows a free-format code block that uses indenting to make the program logic more apparent.

Introducing Free-Format RPG IV, Part 1 - Figure 2

Listing 4-2: Example of indenting free-format calculations

A Note About Case

RPG IV has no rules regarding the case of variables, operation codes, and comments in source statements, but some programmers suggest using a style that capitalizes each “word” in variables and uses lower case otherwise (for example, SubfileRec). Others recommend fully capitalizing all externally defined variables. The compiler translates all variables and operation codes (other than character strings within apostrophes) to upper case before analyzing the code, so whatever case options you choose are purely a personal decision.

More of Jim's introduction to Free-Format RPG IV is coming soon in an upcoming issue of MC RPG Developer. Can't wait?  You can pick up Jim Martin's book, Free-Format RPG IV: Third Edition at the MC Press Bookstore Today!

JIM MARTIN

Jim Martin holds a BS degree in mathematics and an MS in computer science. For 26 years, he was employed by IBM, where he wrote RPG applications for customers and worked in the programming laboratory as a programmer on portions of CPF and the OS/400 operating system. After leaving IBM, Jim took post-graduate work in computer science and performed RPG training. He is an IBM-certified RPG IV developer and author of multiple bestselling editions of Free-Format RPG IV, which, since the book's initial publication in 2005, have taught thousands of RPG IV programmers how to be successful with the free-format coding style.


MC Press books written by Jim Martin available now on the MC Press Bookstore.

Free-Format RPG IV: Third Edition Free-Format RPG IV: Third Edition
Improve productivity, readability, and program maintenance with the free-format style of programming in RPG IV.
List Price $59.95

Now On Sale

Free-Format RPG IV: Second Edition Free-Format RPG IV: Second Edition
>Make the transition from coding in fixed-format RPG to free format.
List Price $59.95

Now On Sale

Functions in Free-Format RPG IV Functions in Free-Format RPG IV
Here’s the ultimate guide to writing RPG IV programs with functions in the free-format style.
List Price $59.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: