Active Forums => QB64 Discussion => Topic started by: Cobalt on December 01, 2021, 12:52:25 pm
Title: A question about Recursion and STATIC arrays
Post by: Cobalt on December 01, 2021, 12:52:25 pm
If I have a Function that I want to attempt recursion with that has a STATIC array defined within it, does the data in the array remain for each recursion or is it a new static array for each time the function recurses?
In my mind I see it as being the same array each time the function is called, either in recursion or by the main loop. As STATIC variables within a sub or function are supposed to remain between calls. I'm just not sure if thats how it actually works during recursion, or if anybody has done this before.
I just don't want to waste a lot of time trying to code this out if its not how things are supposed to work in the first place and then spend countless hours trying to figure out why it doesn't work correctly then.
Of course if there is no experience out there then my option is simply to give it a go and pray for a good result I guess. It however takes less time to ask, than to blindly charge in and try to fix a mangled mess later.
Title: Re: A question about Recursion and STATIC arrays
Post by: bplus on December 01, 2021, 12:58:24 pm
Don't see why the static array would change between calls, recursive or not.
I would feel more comfortable with an array shared with Main though for same functionality.
Title: Re: A question about Recursion and STATIC arrays
Post by: FellippeHeitor on December 01, 2021, 12:59:01 pm
STATIC items retain values across calls, be them recursive or not.
Title: Re: A question about Recursion and STATIC arrays
Post by: Cobalt on December 01, 2021, 01:14:00 pm
then I recurse the function 10 times, will the first call have all 10 elements of the array filled? or will it only have the first element filled? And more so SHOULD it have all 10 elements, since when the first array was called it had only used the first even though by run 10 it has all 9 before it.
I would rather not use a global array on this one so that the code would be copy paste-able without having to add special variables to each program, but if that is how it would have to be then I guess I will go that route.
Title: Re: A question about Recursion and STATIC arrays
Post by: FellippeHeitor on December 01, 2021, 01:51:20 pm
You can easily see all that in action with $Debug, as you'll see in real time where execution is, what level deep you're in and the array contents, but to answer it shortly: as soon as control is returned to the first call, all 10 array items will be filled.
Title: Re: A question about Recursion and STATIC arrays
Post by: FellippeHeitor on December 01, 2021, 01:59:43 pm
It may also be interesting to know that STATIC items in a sub/function are nothing more than SHARED arrays, stored at the main module, but arbitrarily hidden from user interaction. Memory-wise, they work the same.
Title: Re: A question about Recursion and STATIC arrays
Post by: FellippeHeitor on December 01, 2021, 02:00:58 pm
That's also true for non-shared variables at main module: they are only not accessible from subs/functions in QB64 for retrocompatibility. Internally, they are all globally accessible - you can notice that by using $Debug's Watch List feature. All main module variables remain accessible at all times, regardless of scope.
Title: Re: A question about Recursion and STATIC arrays
Post by: bplus on December 01, 2021, 02:43:19 pm
This will illustrate how a recursive function processes before and after calls to itself:
When you see ZZZ... that means it's Sleeping and waiting for keypress.