• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Learn %PARMS! Solve Two CL Problems!

    October 8, 2018 Ted Holt

    Those wonderful people at IBM have done it yet again! They have gladdened my existence with new CL functionality that solves two problems, and I will never have to face those problems again. Let me tell you about the new %PARMS built-in function.

    The %PARMS function returns the number of parameters that are passed into a CL procedure (i.e. a CL program or a CL module). In the past, I have monitored for message MCH3601. That works in some situations, but not in all. The %PARMS function gives me an unambiguous way to know whether a parameter was passed or not. IBM released it for IBM i 7.3 in PTF SI66721.

    I’ll gladly illustrate the two problems and show how to use %PARMS to avoid them.

    The Program Problem

    Consider program PARMTEST:

    pgm (&p1 &p2 &p3)
    
       dcl &p1     *char    3
       dcl &p2     *char    3
       dcl &p3     *char    3
    
       dcl &msg    *char   14
    
       ChgVar  &msg  ('/' *cat &p1 *cat ' ' *cat +
                               &p2 *cat ' ' *cat +
                               &p3 *cat '/')
    
       SndPgmMsg  msg(&msg) MsgType(*comp)
    

    If I call this program and pass it three parameters, I see a message that contains the values I passed in.

    call PARMTEST (AAA BBB CCC)
    /AAA BBB CCC/
    

    But if I call it with fewer than three parameters, I see something else.

    call PARMTEST (AAA BBB)
    MCH3601	Pointer not set for location referenced.
    CPF9999	Function check. MCH3601 unmonitored by PARMTEST at statement 0000000900, instruction X'0000'.
    CPA0702	MCH3601 received by procedure PARMTEST. (C D I R)
    

    The CHGVAR command canceled because I did not supply a parameter value. Here’s how %PARMS allows me to avoid that problem.

    pgm (&p1 &p2 &p3)
    
       dcl &p1     *char    3
       dcl &p2     *char    3
       dcl &p3     *char    3
    
       dcl &x1     *char    3   value('DFT')
       dcl &x2     *char    3   value('DFT')
       dcl &x3     *char    3   value('DFT')
    
       dcl &msg    *char   14
    
       if (%parms *ge 1) then(ChgVar &x1 &p1)
       if (%parms *ge 2) then(ChgVar &x2 &p2)
       if (%parms *ge 3) then(ChgVar &x3 &p3)
    
       ChgVar  &msg  ('/' *cat &x1 *cat ' ' *cat +
                               &x2 *cat ' ' *cat +
                               &x3 *cat '/')
       SndPgmMsg  msg(&msg) MsgType(*comp)
    

    I rewrote the code so that the CHGVAR command no longer refers to the parameter variables, but to local variables instead. I load the local variables from the parameters, but only if those parameters were passed to the program. Otherwise, the local variables have default values.

    call PARMTEST (AAA BBB CCC)
    /AAA BBB CCC/
    call PARMTEST (AAA BBB)
    /AAA BBB DFT/
    call PARMTEST (AAA)
    /AAA DFT DFT/
    call PARMTEST
    /DFT DFT DFT/
    

    Problem number one is solved. Now let me show you problem number two.

    The Module Problem

    Let’s consider the same code, except that PARMTEST is compiled as a module, not a program. Here’s the source code for module CALLER, which calls module PARMTEST.

    pgm
    
       CallPrc  PARMTEST  (AAA BBB CCC)
       CallPrc  PARMTEST  (DDD EEE)
       CallPrc  PARMTEST  (FFF)
       CallPrc  PARMTEST
    
    endpgm
    

    I use the Create CL Module (CRTCLMOD) command (option 15 in PDM) to create both modules and CRTPGM to bind them into one program.

    CRTCLMOD MODULE(MYLIB/PARMTEST) +
       SRCFILE(QCLSRC) SRCMBR(PARMTEST)
    CRTCLMOD MODULE(MYLIB/CALLER) +
       SRCFILE(QCLSRC) SRCMBR(CALLER)
    CRTPGM PGM(CALLER) MODULE(CALLER PARMTEST) +
       ACTGRP(*NEW)
    

    Now I have a program. Here’s what happens when I call it.

    CALL CALLER
    /AAA BBB CCC/
    /DDD EEE CCC/
    /FFF EEE CCC/
    /FFF EEE CCC/
    

    CALLER thinks I passed all three parameters, even when I didn’t. When I don’t supply a parameter, CALLER uses the data from an earlier call, as the pointers to that data just happen to still be in memory. This can certainly lead to erroneous, unpredictable, undesirable results.

    However, if I use the new version of the PARMTEST module instead, I see this:

    CALL CALLER
    /AAA BBB CCC/ 
    /DDD EEE DFT/ 
    /FFF DFT DFT/ 
    /DFT DFT DFT/
    

    Thanks to %PARMS, I was able to use default values for unpassed parameters.

    Will I ever monitor for MCH3601 again? Who knows? It doesn’t look very likely that I will.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: %PARMS, 400guru, CL, FHG, Four Hundred Guru, IBM i

    Sponsored by
    Raz-Lee Security

    Protect Your IBM i and/or AIX Servers with a Free Virus Scan

    Cyber threats are a reality for every platform, including IBM i and AIX servers. No system is immune, and the best defense is prompt detection and removal of viruses to prevent costly damage. Regulatory standards across industries mandate antivirus protection – ensure your systems are compliant and secure.

    Get My Free Virus Scan

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: How Can You Think That? Modern Lessons From A Fresh IBM i Developer

    2 thoughts on “Guru: Learn %PARMS! Solve Two CL Problems!”

    • Michael says:
      October 8, 2018 at 10:48 am

      Typo, MCH3601 not MCH3061 🙂

      Reply
    • Doug S says:
      November 2, 2018 at 1:01 pm

      Is there a version for 7.2?

      Reply

    Leave a Reply Cancel reply

TFH Volume: 28 Issue: 67

This Issue Sponsored By

  • UCG Technologies
  • Rocket Software
  • SEA
  • Computer Keyes
  • WorksRight Software

Table of Contents

  • Riding The Upgrade Cycle
  • Modern Lessons From A Fresh IBM i Developer
  • Guru: Learn %PARMS! Solve Two CL Problems!
  • As I See It: How Can You Think That?
  • The Cloud Changes Things, But Not Everything

Content archive

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

Recent Posts

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • 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

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