简体   繁体   English

递归 function 是如何返回值的?

[英]How recursive function is returning value?

function reverse(str){
    if(str.length <= 1) {
        return str;
    } 
    return reverse(str.slice(1)) + str[0];
}

console.log(reverse("two"))

When the last return str line hits inside the if statement, it returns 'o' .当最后一个return str行在if语句内命中时,它返回'o' Yet there is an extra + str[0] outside the if statement.然而,在if语句之外还有一个额外的+ str[0] Shouldn't it be returning 'oo' rather than 'o' ?它不应该返回'oo'而不是'o'吗?

The function is working completely fine, but when I try to visualize it I get confused. function 工作正常,但是当我尝试将其可视化时,我感到困惑。

A visual representation of the code.代码的可视化表示。 在此处输入图像描述

It will enter the if branch only when the string length is less than or equal to 1只有当字符串长度小于等于1时才会进入if分支

eg.例如。 str = "o" str = "o"

then it will enter然后它会进入

if(str.length <= 1) {
    return str; //here your function will return the answer to main() and will exit this function
} 

OUTPUT OUTPUT

o

Since you are writing return str it will immediately return from that function and will not execute any following lines of code.由于您正在编写return str ,它将立即从该 function 返回,并且不会执行任何以下代码行。 So it will not execute this:所以它不会执行这个:

return reverse(str.slice(1)) + str[0]; //this part will never be reached

The above part will be executed only if the str len>=2仅当str len>=2时才会执行上述部分


Let us now understand the recursion stack现在让我们了解递归堆栈

str  =  "two"

This is how recursion works--这就是递归的工作原理——

reverse(two) = reverse(wo)+ t = reverse(o)+ wt = o+wt = owt

Note: reverse(o) will simply return o as length of string is 1注意: reverse(o)将简单地返回o因为字符串的长度是 1

This is how it happens in order of the function's internal behavior:这是按照函数内部行为的顺序发生的:

  1. reverse('two') is called调用reverse('two')
  2. reverse('two') calls reverse(str.slice(1)) which evaluates to a call to reverse('wo') reverse('two')调用reverse(str.slice(1))计算结果为调用reverse('wo')
  3. reverse('wo') calls reverse(str.slice(1)) which evaluates to a call to reverse('o') reverse('wo')调用reverse(str.slice(1))计算结果为调用reverse('o')
  4. if(str.length <= 1) evaluates to true and the the reverse('o') call returns 'o' to the caller if(str.length <= 1)计算结果为true ,并且reverse('o')调用返回'o'给调用者
  5. The reverse('wo') call returns 'ow' to the caller ( str[0] was 'w' ) reverse('wo')调用返回'ow'给调用者( str[0]'w'
  6. The reverse('two') call returns 'owt' to the caller ( str[0] was 't' ) reverse('two')调用返回'owt'给调用者( str[0]'t'

and the final result is 'owt' , as expected.正如预期的那样,最终结果是'owt'

So in所以在

return reverse(str.slice(1)) + str[0];

str[0] will not be added to reverse(str.slice(1)) until the function call returns.在 function 调用返回之前,不会将str[0]添加到reverse(str.slice(1))中。 and when it returns the value of str[0] will be the value it was when reverse(str.slice(1)) was called.当它返回时str[0]的值将是调用reverse(str.slice(1))时的值。

When the if statement is satisfied and evaluates to true it exits the current function call immediately returning the value 'o' to the caller, since there is a return statement inside the if .if语句满足并计算为true时,它退出当前的 function 调用,立即将值'o'返回给调用者,因为if中有一个 return 语句。

if(str.length <= 1) {
    return str;       // <-- exits here and returns 'o'
}

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

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