Thread: multiple return points

  1. #1
    Unregistered
    Guest

    multiple return points

    Hello

    I was just wandering, if i have code like this.....

    int main ()
    {
    /*declarations*/

    if(xxx)
    return 1;
    else
    p++


    if (lllll)
    return 0;
    else
    p++;


    ./ *similar coding to above */
    .
    .
    .
    .
    retun 1;

    }


    Is that good programming practice? Or is there a way of not having multiple return points?

    thanks

  2. #2
    Unregistered
    Guest
    I think they generally teach in school that multiple returns are bad. However, sometimes they do fit the occasion. There are a lot of different ways to do things. Do you have something specific in mind that you're trying to write? I'll help you.
    -kris

  3. #3
    Unregistered
    Guest
    Well Kris i've done my program already but another person just advised me that i had to many multipl return points in my code.

    At this point in time your help is not required but thanks for the offer anyway

    cheers

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    mulltiple returns have many purposes, but you dont want to have a return at the end of a loop in an if or else loop.

    return 0; for main (1 for errors)

    return int; for integer functions

    (void) nothing for void functions

    return char or int; for char functions
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Unregistered
    Guest
    but say the i had multiple return points in a function other than main that returns 1 or 0, is it good to have lots of these? Are there any other options for implementing these in different sorts of loops?

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Depending on the function's purpose, if you want to cut down on returns you can just handle them on the fly in the loop( or function ). This way your not returning a million different things. If you want the function calling the process to know how everything went though, that's about the only way.

    That's sort of a tough one to explain, you just asked us to explain the entire book of coding return status' to you. Search the web.
    The world is waiting. I must leave you now.

  7. #7
    Unregistered
    Guest
    I agree with Shadow. It sounds like you're working on something
    though. If you want to post what you're working on, we can tell
    you if there's a better way
    -kris

  8. #8
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I personally find multiple returns pretty useful, as they let you quit loops easily and quickly, although i know it's not a good practice.

    Another approach is to use a variable as the return value, change the return value as the function progress, then return the 'return value' whatever it is. Eg:
    Code:
    int retVal = 1; /* assume failure */
    
    if ( num == 1 ) {
       num = 2;
       retVal = 0;
    }
    
    return retVal;

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by Nutshell
    I personally find multiple returns pretty useful, as they let you quit loops easily and quickly, although i know it's not a good practice.
    I also like using them, it seems to help shorten your code. I admit, I don't know what the academics say about using multiple returns.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I admit, I don't know what the academics say about using multiple returns.
    It's a holy war as expected. I feel that multiple returns can make code shorter and easier to understand when used in moderation. Just as with anything, abuse will result in difficult code, but anyone that says multiple returns should be avoided obviously hasn't done a lot of coding.

    -Prelude
    My best code is written with the delete key.

  11. #11
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    In the ideal situation, one would return a value at the end of the function which tells how the function has finished. Avoiding the use of many multiple returns and avoiding large complex functions would lead to implementing many relative small functions which return a value. Depending on that value one would go further or not. So function-structures like these appear:

    Code:
    retval = function_a ();
    retval &= function_b ();
    
    if (retval == TRUE)
    {
        retval = function_c ();
    
        if (retval == TRUE)
        {
            retval = function_d ();
            retval &= function_e ();
    
            if (retval == TRUE)
            {
                retval = function_d ();
            }
        }    
    }
    To avoid function structures like this, one could combine functions. For example:

    Code:
    retval = function_ab ();
    
    if (retval == TRUE)
    {
        retval = function_c ();
    
        if (retval == TRUE)
        {
            retval = function_de ();
    
            if (retval == TRUE)
            {
                retval = function_d ();
            }
        }    
    }
    But it is not always desirable to combine functions, since other functions may only want to use function_a() or function_b().

    Having many small functions, leads to a lot of function calls. And when combining a lot of combined functions, this leads to a lot of duplicate code which is hard to maintain. Since when function_a() changes, you'll also have to change function_ab().

    Therefore I think that multiple returns are okay when used with care.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM