Search code examples
ibm-midrangerpgle

Methods to return a list of values from rpgle procedure


I am looking for method of returning more than one value(like an array) from a subprocedure in rpgle.I don't want to use files etc. to store this value. Can somebody recommend any good method to achieving this?


Solution

  • With only a maximum of 20 values being passed back...

    You could pass the array back directly..

       dcl-proc TestProc;
         dcl-pi *n char(20) dim(20) ;
           parm1 char(20);
         end-pi;
    
         dcl-s myarray char(20) dim(20); 
    
         return myarray;
       end-proc;
    

    Optionally, you could define the proc as returning DIM(200) for instance and pass in a value for how many values you actually want. The compiler will happily truncate the DIM(200) into a DIM(20) when you do the call. This would provide a bit more flexibility. The downside would be performance if you intend to call this 1,000s of times a second. Returning "large" values has some performance penalties.

    Assuming a recent version of the OS, IBM added the RTNPARM keyword to improve the performance of large return values.

       dcl-proc MainProc;
       dcl-s arr char(20) dim(20);
         arr = TestProc(%elem(arr));
         dsply arr(1);
       end-proc;
    
       dcl-proc TestProc;
         dcl-pi *n char(20) dim(200) rtnparm ;
           howmany int(10) value;
         end-pi;
    
         dcl-s myarray char(20) dim(200);
         dcl-s x int(10);
         for x = 1 TO howmany;
             // load array
             myarray(x) = 'Something';
         endfor;
         return myarray;
       end-proc;
    

    Data queue's as David mentioned and Data Area's are other possibilities.

    They might make for more accessibility from another language. But they are a bit hard to use. Luckily, you could always provide a wrapper the converts the returned array into something else.