GOTO to a return in a subroutine amidst multiple If-Then's


Closed Thread
Results 1 to 4 of 4
  1. #1

    Default GOTO to a return in a subroutine amidst multiple If-Then's

    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):

    Code:
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default Re: GOTO to a return in a subroutine amidst multiple If-Then's

    just say "return".

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

    Code:
    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.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: GOTO to a return in a subroutine amidst multiple If-Then's

    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.

  4. #4
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: GOTO to a return in a subroutine amidst multiple If-Then's

    And it saves a few instructions. (faster)
    Dave Purola,
    N8NTA
    EN82fn

Similar Threads

  1. GOTO vs RETURN after a GOSUB
    By rsocor01 in forum PBP3
    Replies: 9
    Last Post: - 25th April 2018, 21:24
  2. jumping out of subroutine with goto
    By helloo in forum General
    Replies: 4
    Last Post: - 3rd January 2012, 11:37
  3. GOTO main or RETURN - Question
    By studysession in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 31st January 2009, 17:13
  4. Does CLEAR Command clear return adrress of a subroutine?
    By sayzer in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 19th February 2008, 17:25
  5. Return VS Goto?
    By Fredrick in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 11th January 2008, 01:13

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts