Shop OBEX P1 Docs P2 Docs Learn Events
Can I use the same routine as a subroutine also by nesting the RETURN command? — Parallax Forums

Can I use the same routine as a subroutine also by nesting the RETURN command?

Philip T.Philip T. Posts: 8
edited 2006-07-20 02:21 in BASIC Stamp
BSII

Can I use the same routine as a subroutine also by nesting the RETURN command?

SwitchCheck:
(BUNCH OF CODE)
BUTTON 8,0,1,1,btn3,1,left
(A BUNCH OF OTHER CODE I DON'T WANT TO RETURN TO)

(LATER IN PROGRAM)
FOR reps=1 TO 150
GOSUB left
NEXT

left:
OUTL = %01100110
PAUSE 20
RETURN
GOTO SwitchCheck

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-07-19 17:53
    Hello,

    ·· Technically when your FOR...NEXT loop completes the routine will fall into the subroutine and try to RETURN even though there was no GOSUB.· This isn't a good idea.· Also, you GOTO SwitchCheck will never be executed.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Philip T.Philip T. Posts: 8
    edited 2006-07-19 21:12
    I Should Have asked this better:
    I added a little more code to better reprsent the problem.
    Will the nested return code mess with the subroutine when it is called from the BUTTON command.
    I believe from your reply that it will.



    SwitchCheck:
    (BUNCH OF CODE)
    BUTTON 8,0,1,1,btn3,1,left
    (A BUNCH OF OTHER CODE I DON'T WANT TO RETURN TO)

    (LATER IN PROGRAM)
    Purge:
    FOR reps=1 TO 150
    GOSUB left
    NEXT

    GOTO SwitchCheck

    (LATER IN PROGRAM)
    ' subroutines
    left:
    OUTL = %01100110
    PAUSE 20
    RETURN
    GOTO SwitchCheck

    If this will cause problems then I will have to create new adresses that will call the subroutine?
    IE:

    SwitchCheck:
    (BUNCH OF CODE)
    BUTTON 8,0,1,1,btn3,1, leftNew
    (A BUNCH OF OTHER CODE I DON'T WANT TO RETURN TO)

    (LATER IN PROGRAM)
    Purge:
    FOR reps=1 TO 150
    GOSUB left
    NEXT

    GOTO SwitchCheck

    (LATER IN PROGRAM)
    ' subroutines
    left:
    OUTL = %01100110
    PAUSE 20
    RETURN

    leftNew:
    GOSUB left
    GOTO SwitchCheck

    I have 8 of these subroutines that I need to use twice depending on conditions and I was trying to reduce code.
    Thank you
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-07-20 01:21
    Your second approach above with the "leftNew" routine is correct.


    Another way to approach is would be to use a program flag:

    R_flag VAR bit
    left:
    ' .... as written
    IF R_flag THEN RETURN ELSE SwitchCheck

    In your purge routine, you would set the R_flag=1, but in the main switchCheck routine you would leave it zero.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Philip T.Philip T. Posts: 8
    edited 2006-07-20 02:21
    Thanks Tracy.
    I will use the flag.
Sign In or Register to comment.