递归幂函数:如果没有初始返回值,为什么它会起作用?
因为除非指数为 0,否则 power(base, exponent) 没有返回值,所以最初 power(base, exponent -1) 不应该返回“未定义”,因此最初是不可乘的吗?所以,我在遵循这段代码的逻辑时遇到了困难。为什么/如何运作?
function power(base, exponent) {
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}
because power(base, exponent) has no return value unless exponent is 0, initially, shouldn't power(base, exponent -1) return 'undefined', and therefore be unmultipliable, initially? So, I am having trouble following the logic of this code. Why/how does it work?
function power(base, exponent) {
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看看如果您尝试计算
5^3
会发生什么:...
power(5, 2)
是什么? ......
power(5, 1)
是什么? ......什么是
power(5, 0)
? ......当我们沿着堆栈向上走时,以相反的顺序将它们放在一起...
Look at what happens if you try to calculate
5^3
:... what is
power(5, 2)
? ...... what is
power(5, 1)
? ...... what is
power(5, 0)
? ...... putting that together, in reverse order as we walk back up the stack...
它可以更简洁:
然而,迭代解决方案要快得多:
It could be more concise:
Howerver an iterative solution is very much faster:
我认为该函数反过来更有意义,如下所示:
if 语句的 return 链接在一起,直到 else 语句< /em> 被执行。
示例
记住它是 if/else 语句的返回,它沿着链向上传递到调用它的函数。
一个有点愚蠢的比喻
假设你想问大卫一个问题,但你又不想大喊大叫,你可以问你旁边的人,谁可以问你旁边的人,谁可以问那个人在你旁边,依此类推,直到这个问题被问到大卫。
只有知道大卫的答案后,这条信息才能被传回到提出问题的人链上。
I think the function makes more sense the other way around, like this:
The return of the if statements are chained together and cannot be resolved until the else statement is executed.
Examples
Remember it is the return of the if/else statements that is being passed back up the chain to the function that called it.
A slightly stupid metaphor
Let's say you want to ask David a question but you don't want to yell, you could ask there person beside you, who could ask the person beside you, who could ask the person beside you, and so on, until the question was asked to David.
Only once David's answer is known can that piece of information be passed back up the chain of people that asked the question.
创建对该函数的调用堆栈。此过程持续进行,直到满足终止条件/“基本情况” - 此处为返回值。此时,所有函数都可以返回,并且原始函数调用将返回答案。换句话说,返回的值将从堆栈中弹出并用于计算下一步(按相反顺序),依此类推,直到堆栈为空。
使用
2^2
示例:power(2, 2);
调用:
这导致:
这导致:
现在它有一个可以使用的返回值,可以这么说,它可以向外工作。
这导致:
这导致:
最终返回 4,即 2^2。
A stack of calls to the function is created. This process continues until it meets a termination condition/"base case" - here, a returned value. At that point, all the functions can return and the original function call returns the answer. Explained in other terms, the returned values are popped out of the stack and used to calculate the next step (in inverse order) and so forth until the stack is empty.
Using a
2^2
example:power(2, 2);
calls:
This leads to:
This leads to:
Now that it has a returned value to work with, it can work back outward, so to speak.
This leads to:
This leads to:
which ultimately returns 4, which is 2^2.
如果指数为负数,所有列出的版本都将不起作用。 Math.pow() 的正确实现版本应该像这样完成 -
All of the listed versions will not work if the exponent is negative. The right version of realization of Math.pow() should be done like this -