Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ARRAY declaration in GOSUB routines.
#12
The problem arises that ERASE... well, it erases arrays.

FOR i = 1 to 3
    DIM x(10)
NEXT

The above is going to create an error for you, as x() is already defined on the first pass around when i = 1.  When i = 2, it can't redim the array as it's already existing.

FOR i = 1 to 3
    REDIM x(10)
NEXT

Now, with the above, there's not going to be any issue.  On the first pass x() gets dimensioned to hold 11 elements.  When i = 11, x gets redimensioned to hold 11 elements -- thus blanking all the old values of x().

FOR i = 1 TO 3
   DIM x(10)
   ERASE x
NEXT

Now, with the above, things are going to work once again as well.  You make x().  You erase x().  You make x().  You erase x().



So where did you run into a problem?

SUB foo
   DIM x(10)   <-- you defined x()
   GOSUB subroutine   <-- you jumped into the gosub
   GOSUB subroutine  <-- you jump into the gosub... but the array has been ERASED
    EXIT SUB 

subroutine:
   PRINT x(1) <-- you try and use the array to do something.  Does that array exist now??
   ERASE x   <--- and then you erased the x()
   RETURN   <-- and then you returned back to the next line
END SUB


And that's why moving the DIM from inside the subroutine to the top of the SUB errored out on you.

Your program was doing exactly what you told it to do for you.  It created an array.  It erased that array.  It then tried to use an array that no longer existed and ERROR ERROR ERROR.
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, 12:25 PM



Users browsing this thread: 1 Guest(s)