Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is return an operator or a function?

Tags:

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?

like image 412
Lazer Avatar asked May 06 '10 12:05

Lazer


People also ask

Can you use or operator in return statement?

It means the same thing there that it means anywhere else. It is the logical or operator. It is not so much "in a return", as much as you evaluate that expression and then return the result of it. It means the return statement returns either 0 or 1 .

Is an operator a function?

An operator function is a user-defined function, such as plus() or equal(), that has a corresponding operator symbol. For an operator function to operate on the opaque data type, you must overload the routine for the opaque data type.

IS function and operator are same?

Here are some differences between an operator and a function: An operator does not push its parameters onto the stack, but a function pushes its parameters onto the stack. The compiler knows about the operation of the operators, but is not aware of the output of the function.

What is return statement in C?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.


2 Answers

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

like image 70
sbi Avatar answered Oct 01 '22 20:10

sbi


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.

like image 43
Will Vousden Avatar answered Oct 01 '22 20:10

Will Vousden