程序:
#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 4:n location 0023feb0 不就已经完了吗?
还有当LEVEL 3:n location 0023fed0 的时候,n不就又小于4了吗,为什么不遵循
if函数了?
因为N=4这次调用是N=3的时候调用的 N=4结束后 不就又返回调用的地方了么?
IF函数都已经运行了 调用N=4的函数不就是在IF语句里面么? 怎么还能再次运行呢?