Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all.. i have a question: the different of return(variable, pointer...) and return x. x: variable, pointer... thanks you!!!
Posted
Comments
Richard MacCutchan 26-Oct-14 4:34am    
No difference. Parentheses may be used to ensure an expression is evaluated correctly.
Stefan_Lang 27-Oct-14 9:53am    
The only context that I can think of where it could make a difference is in macro definitions. Just stay away from those and you'll be fine! ;-)

There is (as Richard has said) no difference: http://msdn.microsoft.com/en-us/library/sta56yeb.aspx[^]

Some older languages did enforce that you had to use parentheses with a return, but you are unlikely to see them now.
So
C++
return x;

and
C++
return (x);
are exactly the same as far as the compiler is concerned.

It's rare to need the parentheses as there is nothing you can add after the statement, but some people do add it for "completeleness" - and they generally always use them, even if it's not a compound return expression. However, it is discouraged as it makes the return statement look like a function call:
C++
int x = return(3);
Can be confused.
C++
int x = return 3;
Cannot be confused.
(Yes, yes, the compiler will complain about both of these, it's just an example...)
 
Share this answer
 
The parentheses around the expression in the return statement do exactly nothing, with the sole exception of decltype(auto)-returning functions (which became part of C++ in C++14) where the parentheses have the same meaning as the parentheses in a regular old decltype.

C++
int n;
int f() 
{
   return n; // returns int
// return (n); same thing
// return ((((n)))); same thing
}

decltype(auto) f()
{
   return n; // returns int, just like decltype(n)
}
decltype(auto) f()
{
   return (n); // returns a reference to int, just like decltype((n))
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900