views:

725

answers:

7

This is too basic I think, but how do both of these work?

return true;   // 1

and

return (true); // 2

Similar: sizeof, exit

My guess:

If return was a function, 1 would be erroneous.

So, return should be a unary operator that can also take in brackets... pretty much like unary minus: -5 and -(5), both are okay.

Is that what it is - a unary operator?

+27  A: 

return is just a language/control flow construct. It's certainly not a function, since it's syntactically irreducible, and it's not really an operator either, since it has no return value.

Will Vousden
@Will Vousden: what is a "control flow construct", apart from what the name tells me?
Lazer
Nice point. If `return` had a `return` value, it would be a little too much recursive ;)
ereOn
@eSKay: A control flow construct is something that alters the flow of program control. See here: http://en.wikipedia.org/wiki/Control_flow `return` could also be considered a language construct, since it is actually part of the language's syntax. See here: http://en.wikipedia.org/wiki/Language_construct
Will Vousden
@eSkay: I've grappled with this concept in other languages and totally understand. What helped me most is to understand that sometimes the language has special things and mechanisms that I as the programmer am not allowed to touch, mimic or simulate through the features the language provides to me. A bad analogy might be: the pilot has more access to features of the plane even though you both fly on the same plane and both access common mechanisms made available like seats, food and life jackets.
John K
.. and the language doesn't have to eat its own dogfood. Oh, I have a lot of these and they're all just as bad. No, actually, they continue to get worse.
John K
+17  A: 

return is not an operator and is not a function. return is a keyword that forms a return statement, which belongs to the category of jump statements. In that regard it has absolutely no similarities with either sizeof or exit.

The requirement to put () around the argument of return existed in ancient pre-standard versions of C (CRM C, for example), but was quickly eliminated, even though the quirky habit to wrap the argument of return in superfluous () can be seen from time to time even today.

AndreyT
@AndreyT: are `sizeof` and `exit` operators?
Lazer
@eSKay: `sizeof` is an operator. `exit` is a stanadard library function.
AndreyT
@AndreyT: okay... so in case of an error condition in my program where I need to abort, should I use `exit(0)` or a `return`? Is there some situation where `exit(0)` should be preferred?
Lazer
@eSKay: `return` will not abort your program. `return` ends the current function, so `return` will only end the program if done in `main`. In that case (in `main`) you can do either. But I see no need for `exit` in `main`, when a plain `return` will do the job.
AndreyT
@AndreyT: thanks!
Lazer
+27  A: 

return is a keyword that manipulates control flow. In that it's similar to if, for etc. It can be used with or without an expression (return; returns from a void function). Of course, as with all expressions, extra parentheses are allowed. (So return (42); is similar to int i = (4*10+2);, in both cases the parentheses are redundant, but allowed.)

sizeof is a keyword that is an operator, similar to new, delete, +, ->, ::, etc.

std::exit() is an identifier that denotes a function of the C standard library (which never returns to the caller).

sbi
I thought `sizeof()` was a macro? Or at least only calculated at compile time?
xyld
nevermind, `sizeof()` was a compile-time keyword in C89, but now is a runtime thing (due to VLA's, yuck!)
xyld
@xyld: It's still a __keyword__ and it's still an __operator__. `:)`
sbi
+7  A: 

return is a control flow keyword, just like goto, break, continue, if, else ... Do not think of it as an operator, because it does not alter the value behind it. The () are just to evaluate expressions and the result of the evaluated expression will be passed along to the calling function (how depends om compiler implementation).

It is also certainly no function, just think about it: how would you return from return?

KillianDS
+3  A: 

"return" is neither a routine nor an operator.

It translates to well known assembler instruction. For example, on the x86 architecture, it translates to "ret", and on the PowerPC, architecture it translates to "blr".

For the value it returns, the compiler moves that value into the appropriate register(s) prior to issuing the return instruction. On the x86 architecture, this is typically EAX and EDX if necessary--the registers will change slightly for x86-64. On PPC, if memory serves, it is r1--others may correct me if I am wrong on that detail.

Hope this helps.

Sparky
As I remember, r1 is the stack pointer and r3 the return value. See figure A-1 here: http://wall.riscom.net/books/proc/ppc/cwg/a_abi.html
Nefrubyr
+2  A: 
'true' is an Expression, 
'(true)' is an Expression. 

return can always be followed by an expression, but for the return to type check, the expression must have the same type of the return type of the function.

hense you can generalize it by saying

return Expression. 

(In a function with a void return type, return may not be followed by an expression; a bare return simply exits the function.)

SysAdmin
A: 

"The requirement to put () around the argument of return existed in ancient pre-standard versions of C (CRM C, for example), but was quickly eliminated, even though the quirky habit to wrap the argument of return in superfluous () can be seen from time to time even today."

Yeah, you know you are looking at some old code or someone thinks return is a function when you see them using parens with it all the time. My college instructor did that and it annoyed me all the time. Oh well at least he was consistent.

daveangel