开发者

Is return an operator or a function?

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 c开发者_如何学JAVAan also take in brackets... pretty much like unary minus: -5 and -(5), both are okay.

Is that what it is - a unary operator?


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).


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.


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.


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?


"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.


'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.)


"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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜