Can I use the same routine as a subroutine also by nesting the RETURN command?
Philip T.
Posts: 8
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
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
·· 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
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
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
I will use the flag.