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.
Listing 4-1: Sample free-format block
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.
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.
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!
MC Press Online