Free-Format Operation Codes
Editor's Note: This article is excerpted from chapter 4 of Free-Format RPG IV: Third Edition, by Jim Martin.
Table 4-1 lists the 62 operations that free-format RPG IV supports as of V7.1. All built-in functions are also available to you in free format. Many built-in functions, such as %Check, %Lookup, and %Scan, provide operation code functionality. Some, such as %Check, provide exactly the same function as an operation code, while others, such as %Lookup, provide additional capabilities.
Perhaps more interesting than which operations are present in free format is which fixed-format operations are not included in the set of free-format operations. Some frequently used fixed-format operations didn’t make the cut.
Table 4-2 lists a sampling.
Most of these operation codes have a free-format counterpart to which you can easily convert. The Seton and Setoff operations, for example, have been replaced by the Eval operation (as in Eval *In21= *On). Another example, Mvr, is easily converted to the %Rem built-in function. And you can replace a Lookup operation with one of five %Lookup or five %Tlookup built-in functions, depending on whether you are searching an array or a table.
It would be nice if all the unsupported operations from fixed format had a simple equivalent in free format, but this is not the case. Some fixed-format operations, such as Move, have no clean and easy conversion path to free format. In Chapter 11, I provide solutions for some of the tough ones (of which, fortunately, there are few). Perhaps one day IBM will provide additional built-in functions or other methods to address the more difficult conversion situations.
In the rest of this section, I describe some of free-format RPG IV’s more commonly used operations (as well as built-in functions where needed) and show some examples to illustrate their use. If you are already writing calculations using the extended Factor 2 format rather than RPG’s original fixed format, you will find much of this material familiar.
Evaluate
The most common operation in free-format RPG IV doesn’t even use an operation code. It’s the evaluate (Eval) operation, minus the Eval operation code. This form of evaluate is usually called an assignment statement. The evaluate operation evaluates the expression specified to the right of the equal sign (=) and assigns the result to the receiving item on the left side of the equal sign. The receiving side is cleared for the default length of the item or for the specified length if substringing is used. This is the way other free-format languages work, too. In fact, CL’s CHGVAR (Change Variable) command functions this way for its VAR parameter.
When performing mathematical calculations, you sometimes want the result rounded (half adjusted). The assignment statement doesn’t perform this rounding, but you can accomplish it by entering the Eval operation code to the left of the assignment statement and specifying the half-adjust operation extender, h. You specify other operation extenders, such as precision (r), in the same way.
Listing 4-3 shows several examples of assignment statements.
Listing 4-3: Sample assignment statements
If
Another popular operation code, supported both in fixed and free format, is the If operation, along with its associated operations Else, Elseif, and Endif. The free-format version of If is similar to the extended Factor 2 method, but it gives you the added freedom of being able to place the specifications anywhere within the line (or lines). Parentheses, in addition to the logical operators And and Or, provide a significant improvement in programming ease over the previous Ifxx operation.
Many people are not aware that RPG had no If at all until RPG III (circa 1979). IBM retrofitted the operation into RPG II around 1990. In case you’re wondering how programmers performed “If” logic in RPG before If was invented, the operation used (and still supported today) was Comp (Compare), along with lots of indicators.
The Elseif operation is a relative newcomer to RPG IV, new with V5R1. Its use can simplify a long set of nested Ifs and Elses. Elseif is equivalent to an Else operation followed immediately by an If. You need only one Endif at the end of an If/Elseif group, regardless of how many Elseifs the group contains. One set of statements may be performed in the If and Elseif blocks of code, or possibly none. This functionality follows the same scheme as the Select and When operations.
Interestingly, you can’t shorten the Endif operation to End in free format. In fact, free format doesn’t support the End operation at all. Correct “Ends” force us as programmers to put the appropriate “Endxx” at the end of a set of logic. It’s a small thing, but program clarity is improved.
Listing 4-4 illustrates the use of If, Else, Elseif, and Endif operations in free format.
Listing 4-4: Examples of If, Elseif, Else, and Endif
Controlled Looping
Controlled looping is another common function in RPG, and IBM made some changes for free format in RPG IV. The most important change is the absence of Goto. To use or not to use a Goto operation (in any programming language) is a topic that programmers have hotly debated. But in free-format RPG IV, there is no debate; Goto simply is not there. To accomplish controlled looping, you use Dou (Do until), Dow (Do while), and For (For) groups.
Doing without Goto can improve the readability and performance of your programs. Knowing that you can’t use Goto forces you to consider other options.
We are still free to code using our personal preferences, but eliminating Goto removes one of the biggest issues in RPG: spaghetti code. For those unfamiliar with the term, “spaghetti code” is code that jumps around in a program using Goto and Tag operations without regard to program structure. Spaghetti code is thus very difficult to maintain. RPG isn’t uniquely vulnerable to spaghetti code; any language that provides a Goto function is at risk. With free-format RPG IV, this issue is behind us.
You may have noticed that another popular looping operation, Do, is missing in free-format RPG IV. This operation is easily converted to the superior For operation, which provides both indexing up to a limit and indexing down from a high starting value to a bottom limit.
Listing 4-5 shows examples of controlled looping in RPG IV.
Listing 4-5: Controlled looping examples
Mathematics
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 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.
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!
LATEST COMMENTS
MC Press Online