C语言递归的返回是返回到什么地方了?

程序:
#include <stdio.h>
void up_and_down(int);
int main (void)
{
up_and_down(1);
return 0;
}
void up_and_down (int n)
{
printf("Level %d: n location %p\n", n, &n);
if (n<4)
up_and_down(n+1);
printf("LEVEL %d: n location %p\n", n, &n);
}

结果
Level 1:n location 0023ff10
Level 2:n location 0023fef0
Level 3:n location 0023fed0
Level 4:n location 0023feb0
LEVEL 4:n location 0023feb0
LEVEL 3:n location 0023fed0
LEVEL 2:n location 0023fef0
LEVEL 1:n location 0023ff10

问题:
在执行完LEVEL 4:n location 0023feb0后,
LEVEL 3:n location 0023fed0
LEVEL 2:n location 0023fef0
LEVEL 1:n location 0023ff10
main()函数和up_and_down()这两个函数之间经历了什么过程才得出了这三句?

结果
Level 1:n location 0023ff10 第一次 传入1 N<4
Level 2:n location 0023fef0 第二次 传入2 N<4
Level 3:n location 0023fed0 第三次 传入3 N<4
Level 4:n location 0023feb0 第四次 传入4 N!<4 不满足条件 所以不会再次递归 输出下面的语句后结束 返回到传入3的时候
LEVEL 4:n location 0023feb0
LEVEL 3:n location 0023fed0 输出后返回到2
LEVEL 2:n location 0023fef0 输出后返回到1
LEVEL 1:n location 0023ff10 结束追问

我能理解求阶称的递归,但是就是不理解这个程序中递归返回的事情。

为什么要返回?Level 4:n location 0023feb0 不就已经完了吗?
还有当LEVEL 3:n location 0023fed0 的时候,n不就又小于4了吗,为什么不遵循
if函数了?

追答

因为N=4这次调用是N=3的时候调用的 N=4结束后 不就又返回调用的地方了么?
IF函数都已经运行了 调用N=4的函数不就是在IF语句里面么? 怎么还能再次运行呢?

温馨提示:答案为网友推荐,仅供参考
相似回答