Thread: Return question

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Return question

    Can return without a value be used in any point of a void function as a kind of goto to the main function ? If so, can there be an unlimted number of returns?
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    [email protected]

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Yes, you can use return by itself to return from a function to the function that called it. There can be as many returns as you want. I assume you are talking about using ifs and then return from each one. For example:
    Code:
    void Function(void){
        if(x==4){
            //Do something
            return;
        }
        else if(x<4){
            //do something else
            return;
        }
        //do something here and end the function
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >There can be as many returns as you want.

    Yes, but realise that when a function has too much exit points, it may get very hard to maintain and read the function. In such cases you could consider to write a new function to handle specific stuff. It is good programming practice to let a function have just one exit points. But sometimes it is more efficient to have more exit points, for example to prevent the function from having a lot of nested if-statements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Union Question
    By estarapapax in forum C Programming
    Replies: 0
    Last Post: 10-21-2008, 10:08 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM