Multiple RETURNS within a single subroutine


+ Reply to Thread
Results 1 to 4 of 4
  1. #1

    Default Multiple RETURNS within a single subroutine

    I am not sure if this is a programming style question a potential stack overflow issue.

    Is it OK to have multiple RETURN statements within a single subroutine? Is there a depth limit? I have tested the multiple return subroutine up to 10 deep with no apparent problems.

    /code
    SubCheck: 'this is the dummy subroutine
    if a = 0 then
    b = 1000
    return
    if a = 1 then
    c = 12*a
    return
    if j = 6 then
    d = 255
    return
    return



    'This is the calling code

    code, code, code
    gosub subcheck
    morecode, morecode

    /endcode

    Cheers
    Brian

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    879

  3. #3
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    562

    Default I use it always

    Hi,

    Since any logic validation executes the return it is as good as only one return.
    Regards

    Sougata

  4. #4
    Join Date
    Apr 2009
    Location
    Pittsburgh, PA
    Posts
    17

    Default It is a question of style

    Having multiple return statements like that has no effect on the stack, the return statement only comes into play when it is executed.

    However it's a programming style question that comes up now and again and one I contemplate again and again.

    When I see horrible code like this:
    Code:
    IF A THEN
      IF  B THEN
        IF C THEN
          IF D THEN
            do something
          END IF
        END IF
      END IF
    END IF
    I want to change it to
    Code:
    IF NOT A THEN
      RETURN
    
    IF NOT B THEN
      RETURN
    ...
    etc
    because from a debugging and comprehension standpoint I find it easier to be able to get out of the processing chain as soon as possible. I don't want to have to work through all those nested IFs wondering what's going to happen next.

    It's easier for me to read, write and test.

Similar Threads

  1. Using "END" in a subroutine?
    By Byte_Butcher in forum General
    Replies: 6
    Last Post: - 13th February 2010, 17:32
  2. Multiple PICS from Same Crystal?
    By WOZZY-2010 in forum General
    Replies: 2
    Last Post: - 6th February 2010, 16:18
  3. Multiple Pics accessing single EEPROM
    By Atom058 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 3rd February 2008, 18:22
  4. subroutine with hserin
    By volcane in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 19th December 2007, 03:56
  5. Replies: 0
    Last Post: - 28th May 2004, 18:25

Posting Permissions

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