Return from within a loop that's inside a function?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Return from within a loop that's inside a function?

  1. #1
    Join Date
    Nov 2010
    Posts
    105

    Question Return from within a loop that's inside a function?

    I have a function that contains a do loop. Can I return from inside the loop? Like this:

    Code:
    int myfunction(char *str, ...){
        ...
        do {...
            n = read(...);
            if (n<0){ // error reading from serial port
                if (errno==EBADF || errno==EIO){
                    return (-1); // exit from function while still inside the do loop, is this OK?
                }
            }
            if (n > 0) { // process read data
               ...
            }
        } while ( ... );
        
        ...
    
        return ntot; // normal exit from function
    }
    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,246

    Re: Return from within a loop that's inside a function?

    Yes. You could have tried and found out for yourself.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Return from within a loop that's inside a function?

    It's certainly possible. Some coding standards recommend using a single-point-of-return, however.

    The main benefit of single-point-of-return is that if you used any resources in the function (opened files, malloc'd memory, locked mutexes, etc) you can organize your code to make absolutely certain that all of those things are released prior to the function return without needing to duplicate the release code in several places.

    On the other hand, if you just use RAII techniques to manage those resources, then they are released automatically on function return anyway, no matter where it is done.

    So in summary: yes, you can do it. Just be cautious to clean up after yourself properly if you do.

  4. #4
    Join Date
    Nov 2010
    Posts
    105

    Re: Return from within a loop that's inside a function?

    Quote Originally Posted by Lindley View Post
    It's certainly possible. Some coding standards recommend using a single-point-of-return, however.

    The main benefit of single-point-of-return is that if you used any resources in the function (opened files, malloc'd memory, locked mutexes, etc) you can organize your code to make absolutely certain that all of those things are released prior to the function return without needing to duplicate the release code in several places.

    On the other hand, if you just use RAII techniques to manage those resources, then they are released automatically on function return anyway, no matter where it is done.

    So in summary: yes, you can do it. Just be cautious to clean up after yourself properly if you do.
    That was the concern. Thank you!

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Return from within a loop that's inside a function?

    Quote Originally Posted by acppdummy View Post
    That was the concern.
    Just because you can do something doesn't mean it's good practice, not even if the code works.

    The consensus view is that it's okay to use multipoint returns with care on occasion. It's definately not good practice to have zillions of returns all over the place.

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center