sir_mud wrote:GOTO hasn't and never will be removed from the default dialect, it has its uses. GOSUB however is generally outdated and only necessary for greater QB compatibility.
Gosub is used in various BASICs for the same reason as Nested SubRoutines. The situation that you need nested subroutines comes up often in math heavy routines.
A Nested subrountine looks like this
Code: Select all
Sub MySub (Arg1 as String, Arg2 as String)
Dim I as Integer
Dim TempI as Integer
Dim Factor as Double
Dim Result as Double
Factor = 10
Result = 0
For I = 1 to 123
TempI = I\ Factor
Result = Result + NestedSub(TempD)
Next I
Function NestedSub(Arg1 as Integer) as Double
Dim I as Integer
Dim TempD as Double
For I =1 to Arg1
TempD = TempD + I/Factor
Next I
Return TempD
End Function
End Sub
Note that any variable declared in MySub is shared between MySub and NestedSub. If a variable is declared in NestedSub that is the same name as in MySub then it becomes local to NestedSub.
Again I found this useful in organizing large and complex series of math heavy subrountines. For example in calculating the unfolded 2d layout of a 3d ductwork fitting.
With nested routines supported the need for GOSUB..RETURN goes away as there is now a construct that works the exact same way.