Understand recursion by considering the implementation function of the function without considering the callback process. If you consider the callback process to understand recursion, you must analyze the parameters and variables in the entire recursive process, and remember when they are called It seems a bit troublesome to call a function and return to the main calling function. The functions of some commonly used recursive functions are well described, such as递归求解阶乘:
The function function is: 计算 n 的阶乘, 函数功能实现就是: n 的阶乘 = n * (n-1)的阶乘. If you understand it this way, you don’t need to consider the function call problem during the recursive process.
For the function in question: The function function is: 输入 n,输出 n+1, 如果 n+1<5,执行 fun(n+1),再输出 n+1(It’s hard to describe in a short sentence): So according to the function description, you can get the execution process of the function, and finally get the result.
Execute the output result of fun(1): 输入 1, 输出 2, 2+1<5,执行 fun(2),得到 fun(2) and then output 2. Final output: 2[output(fun(2))]2
Similarly, fun(2)Output result: 3[Output(fun(3))]3 Final output: 23[Output(fun(3))]32
Similarly, fun(3)Output result: 4[Output(fun(4))]4 Final output: 234[Output(fun(4))] 432
The function is not async so you execute fun(1) 2 $i++; echo $i; 3 callback func(2) $i++; echo $i; 4 callback func(3) $i++; echo $i; 5 Callback func(4) $i++; echo $i; 5 Callback func(4) Because after $i++, it is already greater than 5, so the following echo $i (the bottom echo $i of fun(4)) is executed. fun(4) the entire function execution ends) 4 Since the execution of func(4) is completed, fun(3) will continue to be executed. The bottom echo $i 3 2 also has the same logic
When the fun function is recursive, when it is judged that $i so it outputs 4, then returns to the place where it was pushed onto the stack last time, and continues to output 3... until the fun function returns completely. Changing to if ($i < 5) { return fun($i);} will output 23455
You can think of it this way, what this function does is that the first half echoes i, the second half echoes i, and a call to itself is inserted in the middle until i=5, which is equivalent to echoing i until 5, and then echoing back to the beginning. It’s worth it.. Just have a good relationship with the in and out.
Understand recursion by considering the implementation function of the function without considering the callback process.
If you consider the callback process to understand recursion, you must analyze the parameters and variables in the entire recursive process, and remember when they are called It seems a bit troublesome to call a function and return to the main calling function.
The functions of some commonly used recursive functions are well described, such as
递归求解阶乘
:The function function is:
计算 n 的阶乘
, 函数功能实现就是:n 的阶乘 = n * (n-1)的阶乘
. If you understand it this way, you don’t need to consider the function call problem during the recursive process.For the function in question:
The function function is:
输入 n,输出 n+1, 如果 n+1<5,执行 fun(n+1),再输出 n+1
(It’s hard to describe in a short sentence):So according to the function description, you can get the execution process of the function, and finally get the result.
Execute the output result of
fun(1)
: 输入 1, 输出 2, 2+1<5,执行fun(2)
,得到fun(2)
and then output 2.Final output: 2[output(fun(2))]2
Similarly,
fun(2)
Output result: 3[Output(fun(3))]3Final output: 23[Output(fun(3))]32
Similarly,
fun(3)
Output result: 4[Output(fun(4))]4Final output: 234[Output(fun(4))] 432
Execution
fun(4)
: 由于 4+1<5 判断失败, 不执行fun(5)
, output result: 55Final output: 23455432
When returning recursively, this sentence will be output backwards
The function is not async
so you execute fun(1)
2 $i++; echo $i;
3 callback func(2) $i++; echo $i;
4 callback func(3) $i++; echo $i;
5 Callback func(4) $i++; echo $i;
5 Callback func(4) Because after $i++, it is already greater than 5, so the following echo $i (the bottom echo $i of fun(4)) is executed. fun(4) the entire function execution ends)
4 Since the execution of func(4) is completed, fun(3) will continue to be executed. The bottom echo $i
3 2 also has the same logic
When the fun function is recursive, when it is judged that $i so it outputs 4, then returns to the place where it was pushed onto the stack last time, and continues to output 3... until the fun function returns completely. Changing to
if ($i < 5) { return fun($i);}
will output 23455You can refer to this article, and it will be clear if you understand recursion. http://www.jianshu.com/p/8bee...
You can think of it this way, what this function does is that the first half echoes i, the second half echoes i, and a call to itself is inserted in the middle until i=5, which is equivalent to echoing i until 5, and then echoing back to the beginning. It’s worth it.. Just have a good relationship with the in and out.
Thank you everyone, I understand