Author Topic: A question about Recursion and STATIC arrays  (Read 3488 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
A question about Recursion and STATIC arrays
« 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.
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A question about Recursion and STATIC arrays
« Reply #1 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.

FellippeHeitor

  • Guest
Re: A question about Recursion and STATIC arrays
« Reply #2 on: December 01, 2021, 12:59:01 pm »
STATIC items retain values across calls, be them recursive or not.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: A question about Recursion and STATIC arrays
« Reply #3 on: December 01, 2021, 01:14:00 pm »
now that I re-read it, let me expand just a tad,

lets say I have this function:
Code: QB64: [Select]
  1. FUNCTION ATest (I%)
  2. A(I%)=I%+1
  3. I%=I%+1
  4. IF I%<11 THEN Nul%=ATest(I%)
  5.  

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.
« Last Edit: December 01, 2021, 01:32:27 pm by Cobalt »
Granted after becoming radioactive I only have a half-life!

FellippeHeitor

  • Guest
Re: A question about Recursion and STATIC arrays
« Reply #4 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.
« Last Edit: December 01, 2021, 01:58:39 pm by FellippeHeitor »

FellippeHeitor

  • Guest
Re: A question about Recursion and STATIC arrays
« Reply #5 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.

FellippeHeitor

  • Guest
Re: A question about Recursion and STATIC arrays
« Reply #6 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.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A question about Recursion and STATIC arrays
« Reply #7 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.

Code: QB64: [Select]
  1. dummy = ATest(0, 0)
  2.  
  3. Function ATest (I%, callN)
  4.     Static A(10) As Integer
  5.  
  6.  
  7.     If I% < 0 Or I% > 10 Then Exit Function ' b+ added debug check
  8.     Print "CallN:"; callN
  9.     Print "A() before function chages:"
  10.     For j = 0 To 10
  11.         Print A(j);
  12.     Next
  13.     Print: Print "ZZZ... "
  14.     Sleep
  15.  
  16.  
  17.     A(I%) = I% + 1
  18.     I% = I% + 1
  19.     If I% < 11 Then Nul% = ATest(I%, callN + 1)
  20.  
  21.  
  22.     ' this might be very interesting!
  23.     Print "A() at CallN:"; callN; " after recurs call." ' b+ added debug check
  24.     For j = 0 To 10
  25.         Print A(j);
  26.     Next
  27.     Print: Print "ZZZ... "
  28.     Sleep
  29.  
  30.  
  31.  
  32.