Hello..
Is it smart to return; when void ends?
For instance:
Code:void somefunction() {
dosomething();
return;
}
Printable View
Hello..
Is it smart to return; when void ends?
For instance:
Code:void somefunction() {
dosomething();
return;
}
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.
return with no return value is absolutely proper way to exit void function
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.
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.Quote:
No error issued, but may give you a gentle warning... which is always good.
yes, I'm sureQuote:
Originally Posted by Mario F.
http://msdn2.microsoft.com/en-us/library/k68ktdwf.aspx
Look at the two examples
Code:void functionA ( void )
{
cout << "Red is hot";
}
The first is ok, as is the second use of return. A void function that takes noCode:void funcionB ( void )
{
cout << "Yellow is gold";
return;
cout << "Opps, returned above this cant be read\n";
}
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.
Well, ok. But...
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.Quote:
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.
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
}
Absolutely. On that I agree.
Vart is your avatar somthing to do with the film the sixth sense?
yes, something to doQuote:
Originally Posted by swgh
Cool. Good film, I like M Nightshalymans films. Yoi cant beat unbreakable though for suspense
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.
At best it returns void. Not 0.
robatino's statement was with respect to the global main() function, which returns an int, and which specifically returns 0 if control does not reach a return statement within it.Quote:
At best it returns void. Not 0.
Ah. I'm too distracted it seems. Need a rest.
There have been buggy compilers from time to time that were so broken that they required a return at the end of each function, even those that didn't return anything.
But nowadays, I would personally consider it a very strange habit if a programmer put an explicit return at the end of each function. Waste of a line and 7 characters, and it's so easy to accidentally put it in the wrong place, at least when later rearranging a function.
@vart: But again, you can use if(a != b) and put your code in the if() block without using an empty return.
That's true, Des, but that can get very messy if there are a large number of conditions where you want to short-circuit out.
Also, if there are a large number of lines after the condition, they all have to be indented an extra level, which takes up more space than a single extra return, and I think makes the code harder to understand.Quote:
Originally Posted by XSquared