Search code examples
rpgle

Problems passing parameters to free format RPG Linear-Main program


I am trying to test out the modular free format RPG methodology so we can start actually maintaining our code, rather than swimming in it. I was doing pretty good with creating service programs and modules, and separating my code, when I decided to try using all of the new Dcl and Ctl statements to really remove formatting.

I just tried simply copying the following code from the book "programming in ILE RPG" but when I call it and pass a parameter, the value is always 0 in the program. It's like it doesn't see the input parameter. Is this due to PTF's and RDi versioning, or am I missing something stupid? For the record I'm at V7R1M0 and the RDi is version 9.5.1.0

   ctl-opt Main(Driver);

   dcl-pr Driver Extpgm('THISPGM');
       *n int(5);
   end-pr;

   Dcl-pr Celsius int(5);
     *n int(5);
   END-PR;

   dcl-proc Driver;
     Dcl-pi *n;
       Englishtemp int(5);
     END-PI;

   dcl-s Message char(50);
   dcl-S Metrictemp int(5);
   dcl-s State varchar(8);
   Metrictemp=Celsius(Englishtemp);
   Select;
     When Metrictemp<0;
       State='solid';
     When Metrictemp=0;
       State='Freezing';
     When Metrictemp=100;
       State='boiling';
     When Metrictemp>100;
       State='gaseous';
     other;
       State='liquid';
   ENDSL;

   Message='At '+%char(Englishtemp)+' degrees (' + %Char(Metrictemp)+
   ' Celsius), water is ' + State+'.';

       DSPLY Message;
       Return;
   END-PROC Driver;

   dcl-proc Celsius;
     dcl-pi *n int(5);
       Fahrenheit int(5);
     END-PI;
     dcl-s Temperature int(5);
     eval(h) Temperature = (5/9) * (Fahrenheit - 32 );
     Return Temperature;
   END-PROC Celsius;

Solution

  • The program code is ok. I think you messed up passing the parameter from the command line. You defined everything as int(5) which is an integer with 2 bytes.

    The safest way to pass parameters to an RPG program (especially those which are not of type character) is to use a command.

    CMD  PROMPT('Temperature')
    
    PARM KWD(TEMP) TYPE(*INT2) PROMPT('Temperature in °F')
    

    Note: It is a shame that IBM didn't make it consistent throughout the languages. *INT2 in CMD = INT(5) in RPG