• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • No More Global Variables!

    June 24, 2009 Ted Holt

    Last December, I shared four ways to avoid problems caused by the use of global data in RPG programs. I mentioned that a discussion of i 6.1 (or as I call it, V6R1) data-localization features would have to wait for another day. Well, that day is here.

    With V6R1, it is possible to write programs that have no global identifiers. (By identifiers, I mean variables and constants. By variables, I not only mean scalar variables, but also arrays and data structures.) You only need to change two programming practices.

    First, lose the RPG cycle. It breaks my heart to think of RPG without a cycle. I learned RPG on a S/3 Model 12, did my first production work on the S/34, and later worked on the S/36. I prided myself on my understanding of the RPG cycle. But frankly, I have used so little of the cycle over the past 20 years or so, that getting rid of the rest of it is no big deal.

    To create an RPG program that has no embedded cycle logic, use the MAIN keyword in the H specs. MAIN requires one argument–the name of the controlling subprocedure.

    H Main(AR100)
    H dftactgrp(*no) actgrp(*caller)
    H option(*srcstmt: *nodebugio)
    
    D AR100           pr                  extpgm('AR100')
    D   From                         4a   const
    D   To                           4a   const
    
    P AR100           b 
    D                 pi
    D   From                         4a   const
    D   To                           4a   const
    
      ... etc. ...
    
    P                 e
    
    

    In this example, there is no RPG cycle–no detail calcs, no total calcs, no check for page overflow, no check for control breaks, no matching records, none of that stuff. When this program loads, subprocedure AR100 gets control and runs.

    The second thing you must do to avoid global data is to put file declarations inside of subprocedures.

    P GetCustName     b 
    
    FCustMasterif   e           k disk    usropn
    
    D                 pi
    D   Company                      3p 0 const
    D   Customer                     9p 0 const
    
      ... etc. ...
    
    P                 e
    

    In this example, the customer master file is declared within subprocedure “GetCustName,” and is not directly accessible from other subprocedures. There’s a good bit more to say about the use of files within subprocedures. That will have to wait until yet another day.

    Your program source code consists of H specs, D specs for prototypes and record-format templates, and subprocedures. Subprocedures contain P specs, F specs, D specs, and C specs. The end result is a program with no global identifiers.

    RELATED STORY

    Four Ways to Avoid Problems Caused by Global Data



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Maxava

    Disaster Recovery Strategy Guide for IBM i

    Practical tools to implement disaster recovery in your IBM i environment. Fully optimized to include cloud recovery, replication and monitoring options.

    Download NOW!

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    New Generation Software:  FREE Query Migration Planning Webinar, June 25, 2 p.m. EST
    Tri-Delta Resources:  Free 90 Night Disaster Recovery Service For Your IBM i
    looksoftware:  Why Service-Enable? View the On-Demand Webinar

    IT Jungle Store Top Book Picks

    Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    Chip Wars: List Price, $29.95

    Rational and Happy: IBM Wins Evans’ Development Tool Survey Again Midrange Shops Shift Priorities This Year

    Leave a Reply Cancel reply

Volume 9, Number 21 -- June 24, 2009
THIS ISSUE SPONSORED BY:

Help/Systems
Profound Logic Software
CNX

Table of Contents

  • V6R1 Run SQL Scripts: Print and Save Result Sets
  • No More Global Variables!
  • Reader Input: /QOpenSys Redux, PC5250 Popup Keypads, and Even Farther Beyond Replication

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18
  • Will The Turbulent Economy Downdraft IBM Systems Or Lift It?
  • How IBM Improved The Database With IBM i 7.6
  • Rocket Celebrates 35th Anniversary As Private Equity Owner Ponders Sale
  • 50 Acres And A Humanoid Robot With An AI Avatar
  • IBM i PTF Guide, Volume 27, Number 17

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle