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



Users browsing this thread: 2 Guest(s)