Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ARRAY declaration in GOSUB routines.
#14
ERASE behaves differently inside SUBS than it does in the main module.  It's why I said not to trust its behavior until you can experiment with it for a week or more and see how it interacts in all its variations. 

For instance, see below:

Code: (Select All)
Dim a(3)
a(1) = 1
Print a(1); "prints 1 with no error"
Erase a
Print a(1); "prints 0 as the array has been reset in the main module."
foo
foo2

Sub foo
    Dim a(3)
    a(1) = 1
    Print a(1); "prints 1 with no error"
    Erase a
    Print a(1); "error! the array has been freed and erased in the SUB"
End Sub

Sub foo2 Static
    Dim b(2)
    b(2) = 2
    Print b(2); "prints 2 with no problem"
    Erase b
    Print b(2); " prints 0 with no problem as this array has been reset, not erased"
End Sub

In the main module, ERASE will reset your arrays to null or zero.
In the SUB or FUNCTION, ERASE will free your arrays.
In a SUB or FUNCTION declared STATIC, ERASE only resets those arrays.  (OF course, STATIC affects other things as well, so I wouldn't recommend using it...)

This is all legacy behavior and goes back all the way to QB45.  Is it confusing?  YEP, it sure is!!

And that's why I recommend to just stay away from these old legacy commands.  

I'd rather manually write my own clear/reset routine and KNOW it's going to work in all instances, rather than to have to sit down and try and sort out when and how ERASE or CLEAR or whatnot is going to interact with my code.
Reply


Messages In This Thread
ARRAY declaration in GOSUB routines. - by bartok - 02-04-2024, 01:05 PM
RE: ARRAY declaration in GOSUB routines. - by SMcNeill - 02-05-2024, 01:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  getting the number of dimensions in an array on the fly? madscijr 7 752 09-16-2025, 12:34 AM
Last Post: madscijr
  Quesiton on Dimensioning an Array Dimster 5 1,006 02-06-2024, 01:55 PM
Last Post: Dimster
  Array in an array NasaCow 78 15,787 04-12-2023, 10:53 AM
Last Post: TempodiBasic
  Can images be read from a file into an array? PhilOfPerth 11 2,565 02-17-2023, 03:31 AM
Last Post: TerryRitchie
  Swapping array elements PhilOfPerth 24 3,957 11-19-2022, 12:16 AM
Last Post: Pete

Forum Jump:


Users browsing this thread: 1 Guest(s)