递归和“返回"Javascript 中的语句 [英] Recursion and "Return" Statements in Javascript

查看:40
本文介绍了递归和“返回"Javascript 中的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初学者 javascript-er 在这里...

Beginner javascript-er here...

我遇到了一个问题,我必须使用递归来确定给定的数字 (n) 是否为偶数.这是我的解决方案 - 它通过了所有测试 - 但我想知道一个地方需要返回"这个词 - 请参阅下面的评论:

I came across a problem where I had to use use recursion to find out whether a given number (n) is even. Here was my solution - which passes all tests - but I'm wondering on one piece where the word "return" is needed - see below comments:

var isEven = function(n) {
  if (n < 0){
    n = Math.abs(n);
  }

  if(n === 1){
    return false;
  } else if (n === 0){
    return true;
  } else {
    return isEven(n-2);  // here is my question - can you explain why does this need the 'return' in there?
  }
};

推荐答案

这是我的问题 - 你能解释为什么这需要'返回'吗?

here is my question - can you explain why does this need the 'return' in there?

它与函数调用有关,以及函数的结果如何传递回调用者.

It has to do with function calls, and how the results of a function are passed back to the caller.

暂时忘记递归,让我们看看一对函数.

Forget recursion for a moment, and lets look at a pair of functions.

function foo() {
  return 'foo for you';
}

function bar() {
  foo();
}

如果我像这样调用 bar() 会发生什么:

What happens if I call bar() like so:

console.log(bar());

预期输出:

'undefined'

foo() 被执行,但函数调用的结果被忽略(例如,不保存到变量,也不返回).bar() 没有显式 return 语句,因此根据 ECMA 规范,使用了隐式 return undefined;.

foo() is executed, but the results of the functional call are ignored (e.g. not saved to a variable, nor returned). bar() has no explict return statement, so by the ECMA specification, an implicit return undefined; is used.

看调用栈(注意这里写的像一个栈数据结构,当前函数调用在栈顶):

Looking at the call stack (note that this is written like a stack datastructure with the current function call at the top of the stack):

foo() ==> returns 'foo for you'
bar() ==> returns 'undefined'

这导致 undefined 被传递到控制台输出函数.

Which results in undefined being passed into the console output function.

如果我们像这样修改bar():

function bar() {
 return foo();
}

我们的输出变为:

'foo for you'

foo()的结果作为bar()检查调用栈的结果返回

The result of foo() is returned as the result of bar() examining the call stack

foo() ==> returns 'foo for you'
bar() ==> returns foo() which returns 'foo for you'

回到您的递归示例,如果没有 return 语句,它将执行,但该执行的结果不会向上传递调用堆栈.

Going back to your recursive example, without the return statement, it will execute, but the results of that execution won't be passed up the call stack.

假设没有 return 语句,然后检查调用堆栈,当 n = 4 时.

Let's pretend the return statement is missing, and inspect the callstack, when n = 4.

isEven(0) ==> returns true
isEven(2) ==> returns undefined
isEven(4) ==> returns undefined

isEven(4) = 未定义.错误.

最终的期望值 true 永远不会被传递到调用堆栈.

The ultimate expected value, true never gets passed up the callstack.

如所写,返回值是结果:

As written, with the return value, this is the result:

isEven(0) ==> returns true
isEven(2) ==> returns isEven(0) which returns true
isEven(4) ==> returns isEven(2) which returns isEven(0) which returns true

isEven(4) = true PASSED.

这篇关于递归和“返回"Javascript 中的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆