How to pass stems as procedure arguments in Rexx

Overview

Although the scripting language Rexx (Restructured Extended Executor) offers the possibility to create procedures and to pass parameters to these procedures. The problem is, that a stem can not be passed as a parameter similar to a list in Java.

The reason is that a stem is not stored as a pointer. Instead, a stem represents n different variables.

Let’s have a look at the following simple program. The procedure testFunc should output the length of the stem.

outlist.0 = 4
outlist.1 = "This"
outlist.2 = "is"
outlist.3 = "a"
outlist.4 = "test"
call testFunc(outlist)
exit

testFunc: 
stem = arg(1)
say stem.0
return;

Instead of the length, the output of the procedure is

STEM.0

But there is a solution.

Solution

The solution makes use of the particular property of Rexx that it is an interpreted language. We use the statement "interpret" to interpret other statements at run time and execute it.

So let’s have a look at the same simple program, but now we use the “interpret” statement.

testFunc: 
stem = arg(1)
interpret "say "stem".0"
return;

And now, we get the length (4) of the stem. If you want to iterate over the elements of the stem you can use "interpret" to.

testFunc: 
stem = arg(1)
interpret "x = "stem".0"
do i = 1 to x
interpret "say "stem".i"
end
return;

Now you can modify the content of the stem, add or delete elements.

Limitation

Since the procedure requires access to the stem, this solution does not work when the keyword "procedure" is used in the function definition.

If you have any questions or suggestions, do not hesitate to contact us.

comments powered by Disqus