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.
b = b + ...
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



Users browsing this thread: 5 Guest(s)