Shop OBEX P1 Docs P2 Docs Learn Events
Gosub limit? — Parallax Forums

Gosub limit?

Naphtali MooreNaphtali Moore Posts: 42
edited 2007-05-03 07:57 in BASIC Stamp
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]

Comments

  • Naphtali MooreNaphtali Moore Posts: 42
    edited 2007-05-03 07:14
    OK, I preformed an experiment with 8 levels of gosub. I was able to get to 8th function but the was an error on the return from the 5th function. Here is the code I used:

    ' {$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....)
  • Naphtali MooreNaphtali Moore Posts: 42
    edited 2007-05-03 07:21
    OK! The answer is: Yes there is a limit. It appears to be 4. There is a maximum of 4 nested GOSUB statements. BUT I don't know if this is Stamp or MEMORY dependent. I am using a BS2SX and had no other variable declared, RAM and EEPROM were pretty much open. Im guessing the stamp has 4 dedicated bytes for saving GOSUB addresses. This might be different of other stamps.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-05-03 07:57
    Naphtah -

    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

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sign In or Register to comment.