Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ARRAY declaration in GOSUB routines.
#5
OK I tried to make up an example following your Case B example:
Code: (Select All)
Option _Explicit

_Title "Test array declaration in GoSub" ' b+ 2024-02-04
Dim x As Integer
x = 10
Call say(x)
End
Sub say (n)
    Dim i As Integer

    GoSub test:
    i = 1
    While i <> n
        i = i + 1
    Wend
    Print minus(i) ' here I have to move Dim Minus(1 to 100) to top of sub because Minus must be declared even though the Gosub test is called and array is DIM'd before I actually use the array in the Sub code!!!
    Exit Sub
    test:
     Dim minus(1 To 100) As Integer
    For i = 1 To 100
        minus(i) = 100 - i
    Next
    Return
End Sub
The above is redlined and won't work because the DIM Minus(1 to 100) line is NOT seen in the Say Sub main code area.

OK I fix the little bit of code by moving DIM Minus() array up to declares section of Say sub:
Code: (Select All)
Option _Explicit

_Title "Test array declaration in GoSub" ' b+ 2024-02-04
Dim x As Integer
x = 10
Call say(x)
End
Sub say (n)
    Dim i As Integer
    Dim minus(1 To 100) As Integer
    GoSub test:
    i = 1
    While i <> n
        i = i + 1
    Wend
    Print minus(i) ' here I have to move Dim Minus(1 to 100) to top of sub
    Exit Sub
    test:

    For i = 1 To 100
        minus(i) = 100 - i
    Next
    Return
End Sub

And it works fine! It's a dumb example but does show how you should declare all varaibles and arrays at top of subroutine or main code NOT under the GOSUB line label.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
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 bplus - 02-04-2024, 06:37 PM

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

Forum Jump:


Users browsing this thread: 1 Guest(s)