Page 1 of 1

recursive functions with

Posted: Wed Sep 04, 2013 6:00 am
by John
I have a couple functions that I need to call each other recursively. I need some arrays to not exceed the scope of the function's calling. For example - the second time the same function is called it needs to not mess with the array from the first time (still active) the function is called. So I added the arrays as an optional parameter like so:

1 def fn_a(abc; _,mat _b$)
2 mat _b$(0)
3 fn_build_mat_b(mat _b$)
4 fnend

However when I call this and hit line 2 I get an error 0315. How can I limit this scope of an array to within a function without passing in?

-John Bowman

Posted: Wed Sep 04, 2013 7:40 am
by gtisdale
If an array is an optional parameter to a function and it is not referenced in the call then it essentially does not exist in the funtion called and can not be manipulated within the function. Arrays are always passed to a function as referenced.

To resolve this the array must be dimensioned before the call and passed into the called function. The dimension can be (0) as you have done in your example, but it MUST be a required parameter, not an unreferenced optional parameter.

FNGeorge

Posted: Wed Sep 04, 2013 8:36 am
by John
Unfortunately, Geroge, your solution would take the scope of those arrays outside of the function - which is the goal. With Luis' help we came up with another solution.


1 def fn_a(abc; _,mat_b$*1024)
2 mat_b$(0)
3 fn_build_mat_b(mat b$)
4 mat2str$(mat b$,mat_b$,delim$)
5 ! (and here we process mat_b$ and consume it instead of stepping through mat b$)
6 fnend

since mat b$ is only used long enough to move it into a big string with the proper scope it is not messed up when this happens recursively.

it'd be much easier if we could use both ByVal and ByRef with arrays, but this work around will do for now, provided the size of the arrays are pretty small.

-John

Posted: Wed Sep 04, 2013 8:39 am
by gtisdale
Clever! Great solution.

FNGeorge