• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Overloading Subprocedures

    October 5, 2020 Paul Tuohy

    The ability to overload subprocedures in RPG is something I had been waiting for a long, long time. IBM finally made it available through a Technology Refresh (7.4, TR1 or 7.3 TR7). If you are not familiar with the term, overloading (in RPG) is the ability to create multiple subprocedures of the same name with different implementations.

    Let’s have a look at how overloading might benefit us when it comes to writing programs and subprocedures. This is a portion of a prototype copy member that, amongst others, contains the prototypes of these three subprocedures:

    dcl-pr format_from_Date varChar(10) extProc(*dclCase);
      dateIn date(*ISO) const;
    end-pr;
    
    dcl-pr format_from_Number varChar(10) extProc(*dclCase);
      dateIn zoned(8) const;
    end-pr;
    
    dcl-pr format_from_Character varChar(10) extProc(*dclCase);
      dateIn char(10) const;
    end-pr;
    

    The three subprocedures all do the same thing — return a character representation of a date in MDY format. The difference between the three is in the parameters passed. One takes a date, one takes a number containing a date and one takes a character containing a date.

    This means that when we need to get our date format, we need to know which subprocedure to call based on the data type of the field containing the date.

    dcl-s forDate date(*ISO) inz(D'2020-08-01');
    dcl-s forNum  zoned(8)   inz(20200801);
    dcl-s forChar char(10)   inz('2020-08-01');
    
    dcl-s returnDate char(10);
    
    returnDate = format_from_Date(forDate);
    returnDate = format_from_Number(forNum);
    returnDate = format_from_Character(forChar);
    

    Overloading allows us to define a common prototype that combines the three prototypes we have already defined:

    dcl-pr format_from_Date varChar(10) extProc(*dclCase);
      dateIn date(*ISO) const;
    end-pr;
    
    dcl-pr format_from_Number varChar(10) extProc(*dclCase);
      dateIn zoned(8) const;
    end-pr;
    
    dcl-pr format_from_Character varChar(10) extProc(*dclCase);
      dateIn char(10) const;
    end-pr;
    
    dcl-pr format_Date varChar(10) overLoad( format_from_Date
                                           : format_from_Number
                                           : format_from_Character);
    

    This means that, in our calling program/subprocedure we can now use the same name for all three subprocedure calls, regardless of the parameters.

    dcl-s forDate date(*ISO) inz(D'2020-08-01');
    dcl-s forNum  zoned(8)   inz(20200801);
    dcl-s forChar char(10)   inz('2020-08-01');
    
    dcl-s returnDate char(10);
    
    returnDate = format_Date(forDate);
    returnDate = format_Date(forNum);
    returnDate = format_Date(forChar);
    

    Let’s add a new subprocedure to the mix. This subprocedure will also return a character representation of a date but will have an extra parameter that allows for the specification of the returned date format.

    dcl-pr format_from_Date_With_Format varChar(10) extProc(*dclCase);
      dateIn   date(*ISO) const;
      toFormat char(4)    const;
    end-pr;
    

    Although this new procedure has an extra parameter, it can still be added to the overload list:

    dcl-pr format_Date varChar(10) overLoad( format_from_Date
                                           : format_from_Number
                                           : format_from_Character
                                           : format_from_Date_With_Format);
    

    Which means that we can use the existing (common) name for our new subprocedures:

    dcl-s forDate date(*ISO) inz(D'2020-08-01');
    dcl-s forNum  zoned(8)   inz(20200801);
    dcl-s forChar char(10)   inz('2020-08-01');
    
    dcl-s returnDate char(10);
    
    returnDate = format_Date(forDate);
    returnDate = format_Date(forNum);
    returnDate = format_Date(forChar);
    returnDate = format_Date(forDate: '*MDY');
    

    Isn’t that nice?

    Rules

    These are the rules that apply when defining a prototype with the OVERLOAD keyword:

    • Terminology: the name of the prototype containing the OVERLOAD keyword is the overloaded prototype. The subprocedure names listed in the OVERLOAD keyword are the candidate prototypes
    • You cannot specify parameters for a prototype with the OVERLOAD keyword
    • A prototype with the OVERLOAD keyword does not end with END-PR
    • All the candidate prototypes must have the same (or no) return value type as the overloaded prototype
    • The only other keywords allowed for the overloaded prototype are related to the data type of the return value
    • The candidate prototypes can be any type of prototype. They can be for programs, procedures, and Java methods

    Strict Compiler

    The compiler is very, very strict when it comes to checking parameters. All of the candidate prototypes must be truly unique. For example, the compiler would not allow the following overload procedure. Even though the definition of the parameters are different, the use of the CONST keyword means that either subprocedure could be called.

    dcl-pr oneParm varChar(10) extProc(*dclcase);
      parm_1 char(1) const;
    end-Pr;
    
    dcl-pr twoParm varChar(10) extProc(*dclcase);
      parm_1 char(10) const;
    end-Pr;
    
    dcl-pr formatParm varChar(10) overload(twoParm: oneParm);
    

    The Compile Listing

    The compile list now contains an Overloaded Prototypes section at the end of the listing. The amount of information shown in this section can be controlled using the /OVERLOAD DETAIL or /OVERLOAD NODETAIL compiler directive. The default is /OVERLOAD NODETAIL. The /OVERLOAD compiler directive can be specified multiple times in a source member.

    This is an example of the Overloaded Prototypes section when /OVERLOAD DETAIL is specified:

                        O V E R L O A D E D   P R O T O T Y P E S
         CALLS TO PROTOTYPES FOR FORMATPARM
             CALLED PROTOTYPE                     REFERENCES (D=DETAILS BELOW)
               TWOPARM                                48D
               ONEPARM
         DETAILED DETERMINATION FOR CALLS TO FORMATPARM
           CALL AT STATEMENT 48 COLUMN 14
             SELECTED PROTOTYPE: TWOPARM
         CALLS TO PROTOTYPES FOR FORMAT_DATE
             CALLED PROTOTYPE                     REFERENCES (D=DETAILS BELOW)
               FORMAT_FROM_DATE...
               FORMAT_FROM_NUMBER...
                                                      45D
               FORMAT_FROM_CHARACTER...
                                                      46D
         DETAILED DETERMINATION FOR CALLS TO FORMAT_DATE
           CALL AT STATEMENT 45 COLUMN 14
             ERROR MESSAGES ISSUED FOR PARAMETER 1 FOR FORMAT_FROM_DATE
    *RNF7536 30     45 004500  The type of parameter 1 specified for the call does not
                               match the prototype.
             ERROR MESSAGES ISSUED FOR PARAMETER 1 FOR FORMAT_FROM_CHARACTER
    *RNF7536 30     45 004500  The type of parameter 1 specified for the call does not
                               match the prototype.
             SELECTED PROTOTYPE: FORMAT_FROM_NUMBER
           CALL AT STATEMENT 46 COLUMN 14
             ERROR MESSAGES ISSUED FOR PARAMETER 1 FOR FORMAT_FROM_DATE
    *RNF7536 30     46 004600  The type of parameter 1 specified for the call does not
                               match the prototype.
             ERROR MESSAGES ISSUED FOR PARAMETER 1 FOR FORMAT_FROM_NUMBER
    *RNF7536 30     46 004600  The type of parameter 1 specified for the call does not
                               match the prototype.
             SELECTED PROTOTYPE: FORMAT_FROM_CHARACTER
     * * * * *   E N D   O F   O V E R L O A D E D   P R O T O T Y P E S   * * * * *  
    
    

    And when /OVERLOAD NODETAIL is specified:

                        O V E R L O A D E D   P R O T O T Y P E S
         CALLS TO PROTOTYPES FOR FORMATPARM
             CALLED PROTOTYPE                     REFERENCES (D=DETAILS BELOW)
               TWOPARM                                48
               ONEPARM
         CALLS TO PROTOTYPES FOR FORMAT_DATE
             CALLED PROTOTYPE                     REFERENCES (D=DETAILS BELOW)
               FORMAT_FROM_DATE...
               FORMAT_FROM_NUMBER...
                                                      45
               FORMAT_FROM_CHARACTER...
                                                      46
     * * * * *   E N D   O F   O V E R L O A D E D   P R O T O T Y P E S   * * * * *  
    
    

    The /OVERLOAD compiler directive can be specified multiple times in a source member to indicate the amount of detail shown for individual subprocedure calls.

    Overloaded subprocedures can make the use of subprocedures that little bit easier. I am sure you will find a use for them.

    Paul Tuohy, IBM Champion and author of Re-engineering RPG Legacy Applications, is a prominent consultant and trainer for application modernization and development technologies on the IBM Midrange. He is currently CEO of ComCon, a consultancy firm in Dublin, Ireland, and partner at System i Developer. He hosts the RPG & DB2 Summit twice per year with partners Susan Gantner and Jon Paris.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

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

    Sponsored by
    UCG Technologies

    YOUR ORGANIZATION NEEDS TO BE THINKING DIFFERENTLY ABOUT YOUR BACKUP & DISASTER RECOVERY STRATEGY

    Heightened Concerns of the Industry

    • Inefficient manual backup processes
    • Effectively storing data offsite
    • Developing and testing a concrete disaster recovery plan
    • Efficient access to data in disaster scenario for necessary users
    • Risk of cyber security attack
    • Declining IT staff and resources

    The true cause of the above concerns is the “new normal” that is the COVID-19 pandemic and an organization’s status quo:

    80% of IBM i users currently backup to tape and 40% of companies
    have no DR plan at all.

    DURING THIS UNPRESENTEDED TIME, DON’T WAIT FOR YOUR BUSINESS
    TO SUFFER A DISASTER TO TAKE ACTION.

    The new way to ensure cost-effective safety

    • Automated cloud backup
    • Two remote sites – redundant storage, power, internet pipe, firewalls, etc.
    • Data encryption at all times – in-flight and at-rest
    • Fully managed remote hardware DR, including remote VPN access for necessary users
    • Regularly simulated phishing tests and cyber security training

    Potential “landmines” in solutions to avoid

    • Single point of storage – no redundancy
    • Misleading data analysis, compression/de-dup ratios, sizing of necessary computer resources for backup and DR
    • Large scale cloud storage with difficult recovery
    • Inability to meet RTO/RPO

    Don’t get caught like the many organizations we’ve seen with inefficient
    exposed backup data and no DR plan!

    WHAT VAULT400 HAS TO OFFER

    Backup

    • Native software agent schedules backups to the UCG cloud based on your retention scheme
    • Client data is backed up to two data centers in US or two data centers in Canada
    • AES bit 256 encryption in-flight and at-rest – 0nly client has the encryption key
    • Detailed data analysis to ensure proper sizing

    Disaster Recovery as a Service

    • UCG provides “hands-off” DR – fully managed recovery
    • 60 days remote VPN access available to unlimited users in event of disaster
    • Documented reports to ensure defined SLAs are met

    VAULT400 for IBM BRMS Users

    • Revolutionary BRMS Backup
    • Custom configurations to meet your specific needs
    • Local and Remote Save Sets
    • IPL and Option 21 Full System Saves
    • Secure replication encryption; local archives and tape encryption support
    • Compression & Deduplication reduction up to 100:1
    • Full 24 to 48 hour DRaaS
    • Run remotely for up to 60 days
    • Supports All Platforms and Databases

    Serving the US, Canada, & Latin America

    Visit VAULT400.com/proposal to receive a FREE analysis and proposal

    VAULT400 Cloud Backup & DRaaS is an IBM Server Proven Solution.

    800.211.8798 | info@ucgtechnologies.com| ucgtechnologies.com/cloud

    To the First Responders serving on the front-lines during the COVID-19 pandemic, we extend our heartfelt gratitude.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Seiden Group Helps Bring Community PHP To The Japanese Market IBM Further Extends Service Extension For IBM i 7.1

    3 thoughts on “Guru: Overloading Subprocedures”

    • jc says:
      October 5, 2020 at 9:12 am

      The compiler listing has level 30 errors! What is wrong with the example code you gave us?

      Reply
      • Ralph Daugherty says:
        October 6, 2020 at 6:22 pm

        Paul noted that example is a compile error because the parms for the two overloaded prototypes are const which he mentions is cause of error.

        Reply
    • Ralph Daugherty says:
      October 6, 2020 at 6:34 pm

      This is a nice addition to RPG and a good explanation (as always) from Paul Tuohy. I have occasionally had two or three subroutines with a letter difference in name that typically produced a date from different formats, similar to example given. Or a routine with all possible parms and not all populated with each call.

      So this can make code cleaner for sure, kudos to RPG compiler team (Barbara Morris, etc) for adding this competitive language functionality to RPG.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 30 Issue: 61

This Issue Sponsored By

  • ARCAD Software
  • WorksRight Software
  • UCG Technologies
  • Computer Keyes
  • Manta Technologies

Table of Contents

  • The Fall Technology Refreshes Are Almost Here
  • IBM Further Extends Service Extension For IBM i 7.1
  • Guru: Overloading Subprocedures
  • Seiden Group Helps Bring Community PHP To The Japanese Market
  • The Smearing Of Infrastructure And Platform Clouds

Content archive

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

Recent Posts

  • 2021 Predictions for IBM i, Part 1
  • West Four Stands Out With On Demand Color Label Printing
  • HelpSystems Acquires Data Security, File Transfer Companies
  • Four Hundred Monitor, January 13
  • IBM i PTF Guide, Volume 23, Number 2
  • Seiden Group Unveils A PHP Distro For IBM i
  • Thoroughly Modern: DevOps Refactoring Of RPG Applications with RDi
  • Guru: Fall Brings New RPG Features, Part 2
  • More Vintage Power Systems Feature Withdrawals
  • IBM i PTF Guide, Volume 23, Number 1

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 © 2021 IT Jungle

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.