Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ARRAY declaration in GOSUB routines.
#15
(02-05-2024, 01:24 PM)SMcNeill Wrote: 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.
ok, thank's you! Now that I'm sure there is not a formal mistake, but it is an "ERASE" singularity, I will manually write the code in order to set to "0" those arrays. But it would be interesting to understand why ERASE works like that. I suppose that SUBs existed also at time of QB45, so why they programmed the ERASE command in order to set to "0" o"null" the arrays into the main code and to delete them into a SUB? Mystery.

And im my modest opionion, I don't understand why, considered the lively band who loves QB64 and mainteins it up to date, CLEAR and ERASE commands are so neglected. I undestand that it is possible to clear/erase/set-to-0-null variables, arrays and matrixs with FOR cycles (for example), but it is also possible to clear the screen with a FOR cycle, using SPACE$, but CLS do exists. I think that it is something common to have the necessity to make, in a code, operations that CLEAR/ERASE are intended to do and that brings me to think that simple ERASE/CLEAR well-working commands should be usefull.
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 bartok - 02-05-2024, 02:15 PM

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

Forum Jump:


Users browsing this thread: 1 Guest(s)