简体   繁体   English

返回; 只有没有返回值

[英]return; only with no return value

What does it mean when someone codes: 有人编码时是什么意思:

 template <class T>
 void binaryTree<T>::in_order(Node <T>* node, void (*fun)(T&))
{
    if (node == NULL)   
            return;     //<-- what does this mean here

    inorder(node->left, fun);   //<-- how does this continue here
    f(node->data);
    inorder(node->right,fun);
}

The question is how do you have a return; 问题是你如何得到回报? without returning anything. 没有返回任何东西。 what does this do? 这是做什么的? **Note, I have edited the code above for clarity. **注意,为清楚起见,我已经编辑了上面的代码。

It means that foo(y) is not executed when x == 1 , you return from the function. 这意味着从函数return x == 1时不执行foo(y)

The code continues when x != 1 , if you indent your code and put brackets in the if it'll be clearer: 如果x != 1 ,则代码会继续,如果您将代码缩进并放在方括号中( if会更清晰的话):

void foo(int x, int y) { 
   if(x == 1) {
      return;    //return from the function, don't proceed to foo(y)
   }
   foo(y);   
}

A return statement with neither an expression nor a braced-init-list can be used only in functions that do not return a value, that is, a function with the return type void. 既没有表达式也没有大括号的初始列表的return语句只能在不返回值的函数(即,返回类型为void的函数)中使用。

See this code on Ideone. 在Ideone上查看此代码

If you format it better it might be easier to see: 如果格式化得更好,可能会更容易看到:

void foo (int x, int y)
{ 
    if (x == 1)
        return;
    foo (y);
}

It simply returns if the variable x is equal to 1 . 如果变量x等于1它将简单地返回。 If x is not equal to 1 then the code skips to the recursive call to foo (and I really hope the real code is not like that, because you're calling foo with one argument when it wants two). 如果x 等于1那么代码将跳到对foo的递归调用(我真的希望真实的代码不是那样,因为您在需要两个参数的情况下使用一个参数调用foo )。

这意味着如果x == 1,那么该函数将返回而不执行foo(y)函数;如果x!= 1,则将执行foo(y)函数。

foo is a function. foo是一个函数。

foo takes 2 parameters. foo需要2个参数。 Both parameters are integer . 这两个参数都是integer The parameters are named x and y . 参数分别命名为xy

The body of the function tests whether the value stored in the x variable is equal to 1. If the value stored in x equals 1, then the function will return (exit). 函数主体测试x变量中存储的值是否等于1。如果x存储的值等于1,则函数将返回(退出)。 If the value stored in x isn't equal to 1, then the function calls foo(int x) . 如果x存储的值不等于1,则该函数调用foo(int x)

The foo called inside the function may be different to the foo function that you have supplied the code for (because it has a different interface/prototype). 在函数内部调用的foo可能与您为其提供代码的foo函数有所不同(因为它具有不同的接口/原型)。

What is the body of the second foo (the one that takes a single parameter)? 第二个foo (带有单个参数的那个)的主体是什么? It is possible that the prototype of foo specifies a default value for the parameters x & y (if this is the case then this may be the only foo function. foo的原型有可能为参数x&y指定默认值(如果是这种情况,那么这可能是唯一的foo函数。

Despite the confusing indentation, the return is inside the conditional statement. 尽管缩进令人困惑,但return值仍在条件语句中。

The following is a slightly less confusing way to write that code: 以下是编写该代码的一种不太混乱的方法:

if (x == 1)
   return;
foo (y);

Being inside the conditional statement, return is only executed if x == 1 . 在条件语句中,只有在x == 1时才执行return In all cases, foo(y) is called. 在所有情况下,都会调用foo(y)

If you are used to see the return at last of the code, you can rewrite the code as:: 如果你见惯了return ,在最后的代码,你可以重写代码::

void foo( int x, int y )
{
    if( x != 1 )
    {
        foo( y ) ;
    }
    else /* You can keep it to understand better visually. 
            Even if you don't keep it, the code would do the same */
    {
        return ; 
        /* returns the control to the caller code. 
           Make sure you understand that, it just returns the control but 
           without any value i.e., returning void. */
    }
}

EDIT:: return just returns the control to the caller. 编辑:: return只是将控件返回给调用者。 If you want to return the control with some value, you would return variable_name and if you don't want to return anything, just return . 如果要返回某个值的控件,则将return variable_name ;如果您不想返回任何内容,则只需return

Real-Life Example:: Assume you have been asked to buy some soaps for bath. 现实生活中的示例::假设您被要求购买一些肥皂洗澡。 Now, if soaps are available at the store, you would come back and return me the soaps and caller will go to bath. 现在,如果商店里有肥皂,您会回来将肥皂归还给我,来电者会去洗澡。 This is you are returning back to the caller with some value. 这是您将返回给调用者一些值。 But suppose, if the store is out of soaps, then you would just simply return back to me and I will go to bath without any soap. 但是假设,如果商店里没有肥皂,那么您只需回到我这里,我将不用任何肥皂去洗澡。 This is you are returning back to the caller but without a value. 这是您返回到调用方,但没有值。 In both the cases, you would return to the caller cuz the caller is waiting to proceed further with bathing only on your return. 在这两种情况下,您都将返回到呼叫方,因为呼叫方仅在返回时才等待进一步沐浴。

When a function return type is void , You can return from the function by writing return only. 当函数返回类型为void ,可以通过仅编写return来从函数return no need to give any value after return . return后无需给出任何价值。 If return type would have been int or char etc. then it is compulsory to give a value after return . 如果返回类型是intchar等,则必须在return之后给出一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM