在 void 函数末尾添加 return 语句有什么意义?

发布于 2024-07-15 07:25:29 字数 293 浏览 8 评论 0原文

我看到签名中带有 void return 的函数/方法在函数末尾有 return 语句。 这是什么原因?这是否适用于其他语言?

据我所知,如果我想在函数末尾之外的其他地方退出,我可以使用 return 。

交流示例:

void function(void)
{
   int x = 1 + 2;

   return;    // what do we need this for, if at all?
}

I see functions/methods with a void return in the signature that have a return statement at the end of the function. What is the reason for this, and does this apply to other languages?

For all I know, I can use return if I want to exit anywhere else but the end of the function.

A C example:

void function(void)
{
   int x = 1 + 2;

   return;    // what do we need this for, if at all?
}

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

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

发布评论

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

评论(10

原谅我要高飞 2024-07-22 07:25:29

这在这里似乎毫无意义。 但我的猜测是,这种东西可以用来在不支持在右大括号处放置断点的 IDE 中放置断点,并且通过在此处放置断点,可以在监视窗口等中检查某些值。

This seems pointless here. But my guess is that this kind of thing can be used to put a breakpoint in IDEs which don't support putting a breakpoint at the closing brace and by putting a breakpoint here, some values can be checked in the watch window etc.

你的呼吸 2024-07-22 07:25:29

有人过于字面地理解了“函数必须有一个返回值”的编码准则。

Someone took a 'a function must have a single return' coding guideline too literally.

饮惑 2024-07-22 07:25:29

对于返回 void 的函数来说,这并不重要,因为 void 函数的“从末尾脱落”只是按照您的预期返回。

然而,有些人喜欢这样做以帮助将来的维护。 例如,如果有人后来决定将函数的返回类型更改为 int,如果您忘记更改最终返回语句以使其返回 int 值,您应该会收到编译器错误。 如果您根本没有 return 语句,则返回值是未定义的,并且许多较旧的编译器不会警告您这一点(这是真的 - 我已经看到了!)。 如今这不是什么大问题,因为大多数现代编译器都会对这些事情发出警告。

For a function that returns void, it doesn't matter, since "falling off the end" of a void function simply returns as you would expect.

However some people like to do this to aid future maintenance. For example, if someone later decides to change the return type of the function to say int, you should get a compiler error if you forget to change that final return statement to make it return an int value. If you failed to have a return statement at all, the return value is undefined, and many older compilers wouldn't warn you about this (it's true - I've seen it!). This isn't much of a issue these days, as most modern compilers will warn about these things.

撕心裂肺的伤痛 2024-07-22 07:25:29

我总是在函数末尾包含一个 return 语句,即使它们不返回任何内容。 我最初的原因是我很明确,函数的结束位置没有歧义,我总是对“走出终点”的函数感到厌烦。 后来,这只是一种习惯,很难打破(如果我想的话)。

I always include a return statement at the end of my functions, even when they don't return anything. My original reasons for it were that I was being explicit, there was no ambiguity about where the function ended, I always got irked by functions that "walked off the end". Afterwards, it was simply a habit, hard to break (if I wanted to).

红尘作伴 2024-07-22 07:25:29

正如您自己提到的,如果您想在最后一个语句之前退出函数,则只需要 return 语句。 在给定的示例中,return 语句没有任何作用。

As you mention yourself, you only need the return statement if you want to exit the function before the last statement. In the given example the return statement serves no purpose whatsoever.

ま柒月 2024-07-22 07:25:29

它确实毫无意义......至少对于编译器来说,但是风格上,就像 C++ 中的许多东西一样。 举个例子:

int main ()
{
   DoSomething ();
   return 0;
}

在 C++ 中,对于符合标准的编译器而言,返回 0 是完全没有意义的。 然而有时人们更愿意包含冗余或无意义的陈述只是为了明确。 我不太愿意将这些事情归因于程序员的无知。 这只是风格,如果您愿意,可以随意删除它。

It is indeed pointless... at least to the compiler, but stylistic, much like many things in C++. An example:

int main ()
{
   DoSomething ();
   return 0;
}

In C++ the return 0 is entirely pointless as far as a conforming compiler is concerned. Yet sometimes people prefer to include redundant or meaningless statements just to be explicit. I'd be hesitant to chalk these kinds of things up to programmer ignorance. It's just stylistic, feel free to get rid of it if you feel like it.

甜妞爱困 2024-07-22 07:25:29

它在 C/C++ 中没有用,只会降低可读性。

需要此功能的一个真实语言示例是汇编程序。 但在汇编程序中,return 语句不是语言的一部分。 相反,您必须调用一条特殊指令(x86 中的 ret)来导致控制权返回。 除非你这样做,否则执行就会继续,接下来会发生什么取决于多种因素,包括运气和月相。

It's useless in C/C++ and just reduces readability.

One real language example where this is needed is assembler. But in assembler the return statement is not a part of language. Instead you have to invoke a special instruction (ret in x86) which causes return of control. Unless you do it execution just continues and what will happen next depends on may factors, luck and moonphase included.

嘿看小鸭子会跑 2024-07-22 07:25:29

我的猜测是因为函数过去常常返回一些值,而程序员可能认为它会再次返回。

My guess it is because function used to return some value and a programmer probably thought that it will again.

心如狂蝶 2024-07-22 07:25:29

我不认为您需要它,但您可以拥有它。 尝试将其取出并再次运行该程序。 看看有什么区别。

I don't think you need it, but you can have it. Try taking it out and running the program again. See whats the difference.

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