Reputation: 23
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?
Upvotes: 1
Views: 5527
Reputation: 3674
If the called procedure decides how many elements to return, then if using the return-value mechanism, it would be good to define the return value as a data structure that has the array and the number of values.
copy file
dcl-c MAX_VALUES_RETURNED 20;
dcl-ds values_t qualified template;
num int(10);
arr char(200) dim(MAX_VALUES_RETURNED);
end-ds;
dcl-pr proc likeds(values_t) rtnparm;
...
caller
dcl-ds values likeds(values_t);
values = proc(parms);
for i = 1 to values.num;
... handle values.arr(i)
Upvotes: 5
Reputation: 23823
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.
Upvotes: 4
Reputation: 4014
Consider using a data queue to send a list of values back.
The procedure would send the results to the data queue and return a data queue name (or key for a keyed data queue).
The calling program would then read the data queue (possibly by key, if the queue is pre-existing) and process the entries.
Data queues are fast, easy, and you have the added advantage of being able to use it with other languages ... even if they aren't on the IBM i (java, for example).
Upvotes: 0