void return

This is a discussion on void return within the C++ Programming forums, part of the General Programming Boards category; Hello.. Is it smart to return; when void ends? For instance: Code: void somefunction() { dosomething(); return; }...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    void return

    Hello..

    Is it smart to return; when void ends?

    For instance:

    Code:
    void somefunction() {
      dosomething();
      return;
    }

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,473
    It's not smart. It's kinda like giving an answer when not being asked. It's the type of thing that puts you into trouble in the army.

    However, in C++ the return statement is ignored. No error issued, but may give you a gentle warning... which is always good.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    return with no return value is absolutely proper way to exit void function
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,473
    Didn't even notice the absence of a value. However, are you sure Vart? I always was lead to believe the proper way was to not use the return statement.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    No error issued, but may give you a gentle warning... which is always good.
    A warning would be strange, since a return statement without an expression used in a function that does not return a value is legal according to the C++ Standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Mario F.
    Didn't even notice the absence of a value. However, are you sure Vart? I always was lead to believe the proper way was to not use the return statement.
    yes, I'm sure
    http://msdn2.microsoft.com/en-us/library/k68ktdwf.aspx
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Look at the two examples

    Code:
    void functionA ( void )
    {
       cout << "Red is hot";
    }
    Code:
    void funcionB ( void )
    {
       cout << "Yellow is gold";
    
       return;
      
       cout << "Opps, returned above this cant be read\n";
    }
    The first is ok, as is the second use of return. A void function that takes no
    parameters and returns no value like the two above give the programmer the
    "option" to use return without a value to exit the function. If, like iin the second example, any statement is written under the return, it will not be read. As I am sure you are aware, using return is the proper way to exit a function and return to its caller ie: main. Void functions can have no return statement, like the first example. and like mario suggested, this is perfecly ok too. Usually, if or if not the programmer chooses to add a return statement is a personal preference. Many texts teach to use return to end a void function, although leaving it out makes little difference, as the program will automaticly end the function at the closing brace.
    I'm just trying to be a better person - My Name Is Earl

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,473
    Well, ok. But...

    When the flow of control exits the block enclosing the function definition, the result is the same as it would be if a return statement with no expression had been executed. This is illegal for functions that are declared as returning a value.
    Proper way would be standard enforced. And the standard does not enforce it. It states it can or cannot be used on the case of void functions.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if the return; is the last statement in the void function you can freely avoid it...

    but in most cases it is useful in cases like:
    Code:
    void str_cpy(char* a, char* b)
    {
       if(a == b) return; //nothing to do
    
    //here goes the work
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,473
    Absolutely. On that I agree.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Vart is your avatar somthing to do with the film the sixth sense?
    I'm just trying to be a better person - My Name Is Earl

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by swgh
    Vart is your avatar somthing to do with the film the sixth sense?
    yes, something to do
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Cool. Good film, I like M Nightshalymans films. Yoi cant beat unbreakable though for suspense
    I'm just trying to be a better person - My Name Is Earl

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    It's not necessary to use a return statement before the closing brace of the definition of main() either - if it reaches the end without seeing a return statement, it returns 0 automatically.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,473
    At best it returns void. Not 0.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. ChangeDisplaySettings - Blank?
    By Tonto in forum Windows Programming
    Replies: 13
    Last Post: 12-26-2006, 03:17 PM
  5. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 03:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21