• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Procedure Driven RPG Means Keeping Your Variables Local

    November 6, 2023 Gregory Simmons

    One of the things I love most about procedure driven RPG is that it allows me to keep my variables locally defined. Imagine this horror story that happens all too often in RPG shops.

    Jake: “Hey Gregory, Accounting just called; seems the rebalancing report program is acting up. Didn’t I hear you were working on that the other day?”

    Me: “Ugh, yes. All I had to do was reset this field that was used to show or not show the totaling line between branches. I tested in my library and the fix worked fine.”

    Jake: “Yeah, I had to change that program last month. That program is a beast. What’s worse is it’s called the ‘rebalancing report program,’ but it does a lot more than that. And there’s a ton of global variables.”

    Well, I’ll spare you the gory details, but the lesson here was that the variable he used was also used in other subroutines that had nothing to do with the report.

    Using local variables means that the variables you define inside your procedure are only visible/usable inside that procedure. Thus, you cannot accidentally reference variables inside a procedure in any place other than in that procedure. As was the cause of the horror story from above.

    1	**FREE
    2	dcl-proc calculateTotalDue;
    
    3	dcl-s totalDue packed(10:2);
    4	dcl-s i int(3);
    5	dcl-s itemPrice packed(10:2);
    6	dcl-s quantity packed(5);
    
    7	for i = 1 to 100;
    8	  itemPrice = getPrice(i);
    9	  quantity = getQuantity(i);
    10	  totalDue += itemPrice * quantity;
    11	endfor;
    
    12	return totalDue;
    
    13	end-proc calculateTotalDue;
    

    In this example, the variables itemPrice and quantity are declared locally within the procedure calculateTotalDue. This means that the values of these variables are stored in the procedure’s stack frame and do not need to be copied from the heap each time the procedure is called. This can improve performance, especially if the procedure is called frequently.

    It is important to note that using global variables can make code more difficult to read and maintain. This is because global variables can be accessed from anywhere in the program, which can make it difficult to track where the variables are being used and modified. Therefore, it is important to use global variables sparingly and only when necessary.

    That said, there is a time and a place for global variables. For example, I only use global variables in situations where I will initialize those variables when the program starts up or when the program resets. Another good use case for a global variable is when I have a constant that will be used by multiple procedures.

    Here are some additional tips for using global variables in RPGLE:

    • Avoid using global variables whenever possible. Instead, try to declare variables locally within procedures whenever possible.
    • If you do need to use global variables, give them meaningful names (I prefer to start them with gbl_) and document their usage carefully.
    • Avoid modifying global variables in multiple procedures. If a global variable needs to be modified, then it is generally best to do so in a single procedure. For instance, if you have a global variable called gbl_store_sales_tax, then have one procedure called calculate_store_sales_tax and that procedure is the only procedure that changes that value; all others just use that variable.

    By following these tips for when to use local variables versus global variables, you can help ensure that your code is readable, maintainable, and efficient.

    Until next time, happy coding.

    Gregory Simmons is a software engineer with PC Richard & Son. He started on the IBM i platform in 1994, graduated with a degree in Computer Information Systems in 1997 and has been working on the OS/400 and IBM i platform ever since. He has been a registered instructor with the IBM Academic Initiative since 2007, and holds a COMMON Application Developer certification. When he’s not trying to figure out how to speed up legacy programs, he enjoys speaking at COMMON and other technical conferences, running, backpacking, SCUBA diving, hunting, and fishing.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, FHG, Four Hundred Guru, IBM i, RPG, RPGLE

    Sponsored by
    VISUAL LANSA 16 WEBINAR

    Trying to balance stability and agility in your IBM i environment?

    Join this webinar and explore Visual LANSA 16 – our enhanced professional low-code platform designed to help organizations running on IBM i evolve seamlessly for what’s next.

    🎙️VISUAL LANSA 16 WEBINAR

    Break Monolithic IBM i Applications and Unlock New Value

    Explore modernization without rewriting. Decouple monolithic applications and extend their value through integration with modern services, web frameworks, and cloud technologies.

    🗓️ July 10, 2025

    ⏰ 9 AM – 10 AM CDT (4 PM to 5 PM CEST)

    See the webinar schedule in your time zone

    Register to join the webinar now

    What to Expect

    • Get to know Visual LANSA 16, its core features, latest enhancements, and use cases
    • Understand how you can transition to a MACH-aligned architecture to enable faster innovation
    • Discover native REST APIs, WebView2 support, cloud-ready Azure licensing, and more to help transform and scale your IBM i applications

    Read more about V16 here.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Power Systems Software Gets Its Updates And Tweaks Thoroughly Modern: How X-Analysis Transforms IBM i Challenges Into Solutions

    2 thoughts on “Guru: Procedure Driven RPG Means Keeping Your Variables Local”

    • Chris Pando says:
      November 6, 2023 at 4:11 pm

      When I was in college, the language we used was PL/I. Transitioning to pre ILE/RPG was brutal. My happiest day as a data processing professional was the introduction of ILE, allowing RPG to be a modern language. Scoped variables make for much more reliable code.`

      Reply
    • Greg says:
      November 16, 2023 at 4:10 pm

      30 years since ILE was introduced and the few RPG developers who use it write it like OPM.

      Reply

    Leave a Reply Cancel reply

Content archive

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

Recent Posts

  • With Power11, Power Systems “Go To Eleven”
  • With Subscription Price, IBM i P20 And P30 Tiers Get Bigger Bundles
  • Izzi Buys CNX, Eyes Valence Port To System Z
  • IBM i Shops “Attacking” Security Concerns, Study Shows
  • IBM i PTF Guide, Volume 27, Number 26
  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25

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