Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ARRAY declaration in GOSUB routines.
#13
(02-05-2024, 12:25 PM)SMcNeill Wrote: 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.
I don't understand.

Taking a more simple example:

Code: (Select All)
OPTION _EXPLICIT
CALL ciao
END

SUB ciao
    DIM a%(1)
    a%(1) = 1
    PRINT a%(1)'------> return "1"
    ERASE a%
    PRINT a%(1) '------> get an error.
END SUB

If I well understood (I think not) what you said, the question is the following:
DIM a%(1): creates the array;
a%(1) = 1: set the value "1";
PRINT a%(1): returns correctly "1";
ERASE a%: deletes a%
PRINT a%(1): get an OUT OF RANGE error because a%() do not longer exists.

ok. But if this is true, then even the following code should not work, but it does.

Code: (Select All)
OPTION _EXPLICIT
DIM a%(1)
a%(1) = 1
PRINT a%(1)
ERASE a%
PRINT a%(1) '------> it gives "0".

If ERASE a% delete a%(), why then the second:
PRINT a%(1): brings to "0" and not ERROR?
In this case it doesn't delete the array, but it set the values at "0".

Returning to your example:

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

If would work if i was not a SUB.
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, 12:48 PM



Users browsing this thread: 2 Guest(s)