我如何理解此递归基本代码问题
我正在学习JS中的递归概念,我得到了基本代码返回问题的非常基本的问题。
function factorial(n) {
if (n === 1) {
return 1;
}
return n * factorial(n - 1);
}
因此,这是解释递归的简单阶段示例。
阶乘(5)= 120
我的问题是,为什么n到达1时它不会返回1?在IF语句内部返回之后,第二个返回将继续运行?如果它返回1,第二个返回将继续运行,它将永远执行5 * 1(假设N到达5)???
I'm learning the recursion concept in js, I got a very basic question of the base code returning problem.
function factorial(n) {
if (n === 1) {
return 1;
}
return n * factorial(n - 1);
}
So this is a simple factorial example of explaining the recursion.
factorial(5) = 120
My question is, why it's not returning 1 when the n reaches 1? After the return inside the if statement, the second return will continue to run? And if it return 1, the second return continue to run and it will do 5 * 1 forever (let's say n reaches 5)???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我做了一个小例子,添加了一些日志。添加了属性
缩进
以帮助日志缩进,并更好地了解发生了什么。在递归调用(例如在非递归调用中),当外部函数调用内部函数时,当前变量将存储在内存上的某个位置(在一个堆栈上),然后调用该函数。
当内部函数返回时,值将恢复。
如果您看到了我的示例的日志,则参数
n
会降低每个调用的值,但在内部函数返回后已恢复。回答您的问题:
我的问题是,为什么n到达1时不返回1?
n
到达到达 1 时,函数返回1
,但是该结果将应用于Defortearial(2)
,这些结果在那一刻等待结果,依此类推。在if语句内部的返回后,第二个返回将继续运行?
,如果它返回1,则第二个返回继续运行,它将永远执行5 * 1(假设N到达5)?
no。
n
将乘以下一个呼叫的结果(外呼叫等待内部呼叫):I made a small example, adding some log's. The attribute
indent
was added to help on logs indentation, and for better understand what's happen.On recursive calls (like on non recursive calls), when outer function calls the inner function, the current variables are stored somewhere on memory (on one stack), and then call the function.
When the inner function returns, the values will be restored.
If you see the log's of my example, the parameter
n
decrease the value on each call, but was restored after the inner function returns.Answering to your questions:
My question is, why it's not returning 1 when the n reaches 1?
n
reaches1
, the function returns1
, but that result will applied on defactorial(2)
, that are waiting at that moment for the result, and so on.After the return inside the if statement, the second return will continue to run?
And if it return 1, the second return continue to run and it will do 5 * 1 forever (let's say n reaches 5)???
No. The
n
will be multiplied with the result of next call (the outer call waits for the inner call):分析递归的一种好方法是使用简单的情况,并考虑递归将如何展开。
假设n = 3。
因此,将发生以下迭代:
使递归展开并开始返回的条件是
n == 1
,因此,当发生这种情况时,最后一个函数调用将返回1而不是再次调用自己。但是实际结果将是所有返回操作的总和:(3 *((3-1) *((2-1) * 1)),
因此,阶乘(5)返回120,因为:(5 *(5 *(5) -1) *((4-1) *((3-1) *((2-1) * 1))))== 120。
A good way of analyzing recursion is by using a simple situation, and think how the recursion will unfold.
Lets say n = 3.
So, the following iterations will happen:
The condition that makes the recursion unfold and start returning is
n == 1
, so when that happens, the last function call will return 1 instead of calling itself again.But the actual result will be the sum of all returned operations: (3 * ((3-1) * ((2-1) * 1)))
So, factorial(5) returns 120 because: (5 * ((5-1) * ((4-1) * ((3-1) * ((2-1) * 1))))) == 120.