我可以在返回 void 的函数中使用 return 语句吗?

发布于 2024-08-21 09:58:41 字数 105 浏览 5 评论 0原文

我必须返回到递归的上一级。下面的语法对吗?

void f()
{
   // some code here
   //
   return;
}

I have to return to the previous level of the recursion. is the syntax like below right?

void f()
{
   // some code here
   //
   return;
}

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

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

发布评论

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

评论(9

﹏半生如梦愿梦如真 2024-08-28 09:58:41

是的,您可以从 void 函数返回。

有趣的是,您还可以从 void 函数返回 void。例如:

void foo()
{
  return void();
}

正如预期的那样,这与普通的 return; 相同。这可能看起来很深奥,但原因是模板一致性:

template<class T>
T default_value()
{
  return T();
}

这里,default_value 返回类型 T 的默认构造对象,并且由于能够返回 void,它即使当 T = void 时也能工作。

Yes, you can return from a void function.

Interestingly, you can also return void from a void function. For example:

void foo()
{
  return void();
}

As expected, this is the same as a plain return;. It may seem esoteric, but the reason is for template consistency:

template<class T>
T default_value()
{
  return T();
}

Here, default_value returns a default-constructed object of type T, and because of the ability to return void, it works even when T = void.

泡沫很甜 2024-08-28 09:58:41

当然。您只是不应该返回实际值。

Sure. You just shouldn't be returning an actual value.

断爱 2024-08-28 09:58:41

是的,您可以使用该代码从函数返回。

Yes, you can use that code to return from the function.

本王不退位尔等都是臣 2024-08-28 09:58:41

是的,有时您可能希望返回 void() 而不是什么都不返回。

考虑一个 void 函数,它想要调用一些传递 void 函数,而不需要一堆 if-else。

return 
  InputEvent == E_Pressed ? Controller->Grip() :
  InputEvent == E_Released ? Controller->Release() :
  InputEvent == E_Touched ? Controller->Touch() : void();

Yes, sometimes you may wish to return void() instead of just nothing.

Consider a void function that wants to call some pass-through void functions without a bunch of if-else.

return 
  InputEvent == E_Pressed ? Controller->Grip() :
  InputEvent == E_Released ? Controller->Release() :
  InputEvent == E_Touched ? Controller->Touch() : void();
星軌x 2024-08-28 09:58:41

是的,这将从函数返回到上一个递归级别。这将是非常基本的解释,但是当您调用函数时,您正在创建一个新的调用堆栈。在递归函数中,您只需添加到该调用堆栈即可。通过从函数返回,无论是否返回值,都会将堆栈指针移回到堆栈上的前一个函数。它有点像一堆盘子。你不断地在上面放盘子,但返回时会移动顶板。

您还可以使用调试器来验证这一点。只需在代码中放置几个​​断点并单步执行即可。您可以自己验证它是否有效。

Yes, that will return from the function to the previous level of recursion. This is going to be very basic explanation, but when you call a function you are creating a new call stack. In a recursive function you are simply adding to that call stack. By returning from a function, whether you return a value or not, you are moving the stack pointer back to the previous function on the stack. It's sort of like a stack of plates. You keep putting plates on it, but than returning moves the top plate.

You could also verify this by using a debugger. Just put a few break points in your code and step through it. You can verify yourself that it works.

风吹雪碎 2024-08-28 09:58:41

简单的答案是肯定的! C++ 将 void 方法识别为不返回的函数。它基本上告诉编译器,无论发生什么,一旦你看到 return; 就中断并离开该方法......

The simple answer to this is YES! C++ recognise void method as a function with no return. It basically tells the compiler that whatever happens, once you see the return; break and leave the method....

只有影子陪我不离不弃 2024-08-28 09:58:41

您不必在那里返回,程序将自行返回到上一个函数,进入调试模式并单步执行,您可以自己看到它。
另一方面,我认为返回那里根本不会损害该计划。

You shouldn't have to have the return there, the program will return to the previous function by itself, go into debug mode and step through and you can see it yourself.
On the other hand i don't think having a return there will harm the program at all.

倥絔 2024-08-28 09:58:41

正如其他人所说,是的,你可以。在此示例中,返回不是必需的,而且其用途值得怀疑。我认为你指的是函数中间的提前返回。您也可以这样做,但这是不好的编程习惯,因为它会导致复杂的控制流(不是单入口单出口),以及诸如break之类的语句。相反,只需使用 if/else() 等条件语句跳过函数的其余部分。

As everyone else said, yes you can. In this example, return is not necessary and questionably serves a purpose. I think what you are referring to is an early return in the middle of a function. You can do that too however it is bad programming practice because it leads to complicated control flow (not single-entry single-exit), along with statements like break. Instead, just skip over the remainder of the function using conditionals like if/else().

冰火雁神 2024-08-28 09:58:41

是的,例如:

void foo()
{
    // do smth
}

然后

void bar(bool condition)
{
    if (condition) {
        return foo(); //its ok
    }
    // else do smth
}

Yes, for example:

void foo()
{
    // do smth
}

and then

void bar(bool condition)
{
    if (condition) {
        return foo(); //its ok
    }
    // else do smth
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文