Free Ride: A look at free-form calc support

OS/400 V5R1 is one of RPG's biggest releases in terms of functions added to the language. That's really saying something, considering the number of major releases in RPG IV's relatively short lifespan. This month, we'll cover one of these functions: free-form calc specs. We'll save the rest for future issues.

Since the beginning, RPG IV has provided the capability to use a free-format Factor 2 in calc specs, using operation codes such as EVAL, IF, etc. Many RPG programmers began to develop a taste for this limited amount of free-form RPG logic. Most new (or significantly enhanced) RPG IV programs today contain many more lines of calcs using extended Factor 2 than the "old-fashioned" Factor 1/Factor 2/Result types of calcs. Each new release added a variety of built-in functions (BIFs) to entice developers into the free-format world. This is great until you examine your editor screen and notice an expanse of virtually unused space in your calc specs to the left of your operation code. This is particularly frustrating when you're working with a fairly complex expression and realize you must extend the expression to yet another line. The capability to use all of the wasted space on the left would be useful.

That's one of the reasons we like the free-form calc support in V5R1. It's really free--not just Factor 2. The entire calc spec is free format, not even a "C" in column 6. That means complex expressions require fewer lines than before. And you can now indent your RPG logic to your heart's content. No special tools or compile listings are required to help you keep your If/Else/EndIf type logic obvious--you can code it in the source member. Here's an example:

   READ PayRollX;  // Read first record to prime the DO loop
   DOW Not %EOF(PayRollX);
     RecordCount = RecordCount + 1;

    If Hours <= 37.5;
      Pay  = Hours * Rate;
      Eval(H) Pay  = (37.5 * Rate) + ((Hours - 37.5)
                      * (Rate * 1.5));

     // Concatenate first and last name
    FLName = %TrimR(LName) + ', ' + FName;


    If RecordCount = MaxPage;
      Leave;           // Max records reached so exit loop
       READ PayrollX;

  ENDDO;              // Loop back if not EOF

Ironically, the only two lines of code that must start in a specific position are the compiler directives /FREE and /END-FREE, which begin in column 7. The lines in between can use any columns between 8 and 80, allowing you to logically indent your code as we did here. Notice that even operations like Read, Dsply and BegSR can be used in free format.

For all operations, the format is Operation code, followed by Factor 1, Factor 2 and Result. Any Factors that aren't used in a statement are omitted. Resulting (right side) indicators and fields defined following the Result aren't supported in free form. Now, aren't you glad you now code all of your field definitions on the D spec and use BIFs to determine end of file or not found conditions? Conditioning (left side) indicators aren't supported (and hopefully we're banned from your code years ago).

















Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.

comments powered by Disqus



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters