return 是运算符还是函数?

发布于 2024-08-31 08:59:42 字数 453 浏览 9 评论 0原文

我认为这太基础了,但这两者是如何工作的呢?

return true;   // 1

return (true); // 2

类似的:sizeofexit

我的猜测:

如果 return 是一个函数,则 1 将是 错误。

所以,返回应该是一个一元 运算符 也可以接受 括号...很像一元 减号-5-(5),两者都是 好的。

这就是一元运算符吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清风无影 2024-09-07 08:59:42

return关键字操纵控制流。它类似于 iffor 等。它可以与或不与表达式一起使用(return;void 返回 函数)。当然,与所有表达式一样,允许使用额外的括号。 (因此 return (42); 类似于 int i = (4*10+2);,在这两种情况下,括号都是多余的,但也是允许的。)

sizeof 是一个关键字,它是一个运算符,类似于newdelete+->::

std::exit()是一个标识符,表示 C 标准库的函数(永远不会返回给调用者)。

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

作死小能手 2024-09-07 08:59:42

return 只是一种语言/控制流构造。它当然不是一个函数,因为它在语法上是不可约的,而且它也不是真正的运算符,因为它没有返回值。

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.

赢得她心 2024-09-07 08:59:42

return 不是运算符,也不是函数。 return 是构成return 语句的关键字,属于跳转语句类别。在这方面,它与 sizeofexit 完全没有相似之处。

在 C 的古代预标准版本(例如 CRM C)中存在将 () 放在 return 参数周围的要求,但很快就被消除了,尽管即使在今天,将 return 参数包装在多余的 () 中的奇怪习惯仍然时常可见。

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.

情域 2024-09-07 08:59:42

return 是一个控制流关键字,就像 gotobreakcontinueifelse< /code> ...不要将其视为运算符,因为它不会改变其背后的值。 () 只是计算表达式,计算表达式的结果将传递给调用函数(如何取决于编译器实现)。

它当然也不是函数,只要想一想:你会如何从 return 返回?

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?

执妄 2024-09-07 08:59:42

“return”既不是例程,也不是运算符。

它转换为众所周知的汇编指令。例如,在 x86 架构上,它翻译为“ret”,在 PowerPC 架构上,它翻译为“blr”。

对于它返回的值,编译器在发出返回指令之前将该值移动到适当的寄存器中。在 x86 架构上,如果需要的话,这通常是 EAX 和 EDX ——对于 x86-64,寄存器会略有变化。在 PPC 上,如果没记错的话,它是 r1——如果我在这个细节上错了,其他人可能会纠正我。

希望这有帮助。

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

锦欢 2024-09-07 08:59:42
'true' is an Expression, 
'(true)' is an Expression. 

return 后面始终可以跟一个表达式,但对于返回类型检查,表达式必须与函数的返回类型具有相同的类型。

因此,您可以这样概括它

return Expression. 

:(在具有 void 返回类型的函数中,return 后面可能不跟有表达式;裸露的 return只需退出该函数即可。)

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

定格我的天空 2024-09-07 08:59:42

“将 () 放在 return 参数周围的要求存在于 C 的古代预标准版本(例如 CRM C)中,但很快就被消除了,尽管将 return 参数包装在多余的 () 中的奇怪习惯可以即使在今天,也会时不时地看到。”

是的,你知道你正在查看一些旧代码,或者当你看到有人一直使用括号时,他们认为 return 是一个函数。
我的大学老师就这么做了,这一直让我很恼火。
哦,好吧,至少他是一致的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文