Amo*_*ngh 0 c recursion function
这是一个递归程序.但我不明白在这个节目中发生的事件顺序
#include<stdio.h>
count(int);
main()
{
int x=5;
count(x);
}
count(int y)
{
if(y>0)
{
count(--y);
printf("%d ",y);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是:
4 3 2 1 0 ...
Run Code Online (Sandbox Code Playgroud)
但是我没有得到第一次count(5)调用时和调用count(4)时会发生什么.控件是否立即进入函数的开头?或者首先打印出值,y然后再次进入函数的开头count()?
它就像一堆菜.
1 2 3 4 5
count(0)
count(1) count(1)
count(2) count(2) count(2)
count(3) count(3) count(3) count(3)
main main main main main
count(0) prints nothing
Run Code Online (Sandbox Code Playgroud)
转到第4步
count(1) prints 1
Run Code Online (Sandbox Code Playgroud)
转到第3步
count(2) prints 2 ...
Run Code Online (Sandbox Code Playgroud)
所以要获得输出4 3 2 1你需要交换count(--y)和printf("%d",y)线.