Gosub limit?
Naphtali Moore
Posts: 42
Is there a limit to the number of GOSUB you can call before a return....
EXAMPLE:
[noparse][[/noparse]code]
Function1:
·GOSUB Function2
Return
Function2:
·GOSUB Function3
Return
Function<n>:
·GOSUB Function<n+1>
Return
...
[noparse][[/noparse]/code]
EXAMPLE:
[noparse][[/noparse]code]
Function1:
·GOSUB Function2
Return
Function2:
·GOSUB Function3
Return
Function<n>:
·GOSUB Function<n+1>
Return
...
[noparse][[/noparse]/code]
Comments
' {$STAMP BS2sx}
' {$PBASIC 2.5}
DEBUG "Starting Program", CR
GOSUB function1
DEBUG "Program ending", CR
END
Function1:
DEBUG "Entered function 1", CR
GOSUB Function2
DEBUG " Returned from Function 2", CR
RETURN
DEBUG "ERROR between functions", CR
Function2:
DEBUG "Entered function 2", CR
GOSUB Function3
DEBUG " Returned from Function 3", CR
RETURN
DEBUG "ERROR between functions", CR
Function3:
DEBUG "Entered function 3", CR
GOSUB Function4
DEBUG " Returned from Function 4", CR
RETURN
DEBUG "ERROR between functions", CR
Function4:
DEBUG "Entered function 4", CR
GOSUB Function5
DEBUG " Returned from Function 5", CR
RETURN
DEBUG "ERROR between functions", CR
Function5:
DEBUG "Entered function 5", CR
GOSUB Function6
DEBUG " Returned from Function 6", CR
RETURN
DEBUG "ERROR between functions", CR
Function6:
DEBUG "Entered function 6", CR
GOSUB Function7
DEBUG " Returned from Function 7", CR
RETURN
DEBUG "ERROR between functions", CR
Function7:
DEBUG "Entered function 7", CR
GOSUB Function8
DEBUG " Returned from Function 8", CR
RETURN
DEBUG "ERROR between functions", CR
Function8:
DEBUG "Entered function 8", CR
DEBUG " *** END of chain ***", CR
RETURN
DEBUG "ERROR between functions", CR
Here's the output I got:
Starting Program
Entered function 1
Entered function 2
Entered function 3
Entered function 4
Entered function 5
Entered function 6
Entered function 7
Entered function 8
*** END of chain ***
Returned from Function 8
Returned from Function 7
Returned from Function 6
Returned from Function 5
Starting Program
(and it starts all over again....)
Feel free to experiment, but if you had looked at the PBASIC Reference Manual, or the PBASIC Help File, you would have found the following:
quote
GOSUB Restrictions
Only a limited number of GOSUBs are allowed per program, and they may be nested only four levels deep. In other words, the subroutine that's the destination of a GOSUB can contain a GOSUB to another subroutine, and so on, to a maximum depth (total number of GOSUBs before the first RETURN) of four. Any deeper, and the program will "forget" its way back to the starting point (the instruction following the very first GOSUB).
end quote
This is true for all PBASIC Stamps.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔