PDA

View Full Version : GOTO to a return in a subroutine amidst multiple If-Then's



picster
- 26th October 2018, 16:01
Are there any caveats to using multiple GOTOs to a single return from within IF-THEN-ELSE's in a subroutine? I am using it to skip irrelevant code once the first condition is met.

Example (for reference only, not trying to actually do anything):



carry_out_logic:

if counter=4 then
toggleme=~toggleme
LED_4=1
goto continueon
endif

if counter=6 then
LED_6=1
goto continueon
endif

(...other if-then stuff...)

continueon:
return


I know I can make use of ELSIF to skip irrelevant code, but it just gets really messy to read when dealing with multiple conditions in multiple IF's.

sayzer
- 26th October 2018, 16:24
just say "return".

Whenever it is hit, it will return to last call.



carry_out_logic:

if counter=4 then
toggleme=~toggleme
LED_4=1
return
endif

if counter=6 then
LED_6=1
return
endif

(...other if-then stuff...)


return ' if nothing executed so far, it returns anyway.

mpgmike
- 26th October 2018, 18:05
Your approach to a JUMP sequence at the bottom of a subroutine comes in handy when programming in ASM on older PICs that require Context saving, or when there is one or more other tidy-up duties required before a RETURN, but sayer's method works and it's clean.

Dave
- 26th October 2018, 18:13
And it saves a few instructions. (faster)