什么是真正发生在这个code? [英] What is really happening in this code?

查看:104
本文介绍了什么是真正发生在这个code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code,其中包括一个递归函数。我已经浪费了很多时间递归,但我仍然无法得到它真的:

I have a code which includes a recursive function. I have wasted a lot of time on recursion but I still couldn't get it really:

#include<stdio.h>
void count(int);

int main()
{
    int x=10,z;
    count(x);
}

void count(int m)
{
    if(m>0)
        count(m-1);
    printf("%d",m);
}

在第一时间计数被调用,参数为10。它满足了条件,然后从这里开始递归部分。会发生什么真当一个函数调用本身呢?我不明白这一点。请参照栈解释。

When the 1st time count is called with argument as 10. it fulfills the condition and then here starts the recursive part. what happens really when a function calls itself? I don't get it. Please explain with reference to stacks.

推荐答案

M 大于0,我们称之为计数。这里是调用堆栈的重新presentation:

While m is greater than 0, we call count. Here is a representation of the stack calls:

 count (m = 10)  
   count (m = 9)  
     count (m = 8)  
       count (m = 7)  
         count (m = 6)    
           count (m = 5)     
             count (m = 4)     
               count (m = 3)     
                 count (m = 2)     
                   count (m = 1)
                     count (m = 0)
                     printf 0
                   printf 1
                 printf 2
               printf 3
             printf 4
           printf 5
         printf 6
       printf 7
     printf 8
   printf 9
 printf 10

这篇关于什么是真正发生在这个code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆