Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c语言中使用函数任何人都可以解释_C - Fatal编程技术网

如何在c语言中使用函数任何人都可以解释

如何在c语言中使用函数任何人都可以解释,c,C,在这段代码中,为什么有n次打印,请解释为什么打印调用n次 int count(int n){ print("%d" , n); if(n>1){ count(n-1); } print("Integer value is %d\n" , n); return n; } int main(){ count(3); } 在给定的代码

在这段代码中,为什么有n次打印,请解释为什么打印调用n次

 int count(int n){
        print("%d" , n);
             if(n>1){
                  count(n-1);
              }
        print("Integer value is %d\n" , n);
     return n;
    }
   int main(){
     count(3);
  }

在给定的代码中,函数是递归的。Count(n-1)一次又一次地调用该函数,直到条件if(n>1)失败为止。因此,如果将5传递给count函数。它打印5次。

在给定的代码中,函数是递归的。Count(n-1)一次又一次地调用该函数,直到条件if(n>1)失败为止。因此,如果将5传递给count函数。打印5次。

对于计数(n),您的代码将打印如下内容:

n,n-1,n-2....1 Integer value is 1
Integer value is 2
Integer value is 3
....
....
Integer value is n
n、 n-1,n-2…1整数值为1
整数值为2
整数值为3
..
..
整数值为n
现在让我们看看原因,找到对count(3)的递归调用以进行说明:

计数(3)——打印(3)
     (3>1)为真---计数(2)--打印(2)
               (2>1)为真---计数(1)--打印(1)
                          (1>1)为假exec prev func call
               打印(整数值为2) 返回2
     打印(整数值为3)
     返回3
在递归树中,查看代码打印值的位置

Input: count(3)
Output:
321Integer value is 1
Integer value is 2
Integer value is 3
输入:计数(3)
输出:
321整数值为1
整数值为2
整数值为3
对于计数(n),您的代码将打印如下内容:

n,n-1,n-2....1 Integer value is 1
Integer value is 2
Integer value is 3
....
....
Integer value is n
n、 n-1,n-2…1整数值为1
整数值为2
整数值为3
..
..
整数值为n
现在让我们看看原因,找到对count(3)的递归调用以进行说明:

计数(3)——打印(3)
     (3>1)为真---计数(2)--打印(2)
               (2>1)为真---计数(1)--打印(1)
                          (1>1)为假exec prev func call
               打印(整数值为2) 返回2
     打印(整数值为3)
     返回3
在递归树中,查看代码打印值的位置

Input: count(3)
Output:
321Integer value is 1
Integer value is 2
Integer value is 3
输入:计数(3)
输出:
321整数值为1
整数值为2

整数值为3,打印6次。这是,因为节目是这么说的。让我们在主代码中插入函数调用:

int count(3) // call with 3
{
  print("%d" , 3);                      // prints 3
  if(3>1)
  {
    count(3-1);
  }
  print("Integer value is %d\n" , n);   // prints 3
  return n;
}
再次插入呼叫:

int count(3) // call with 3
{
  print("%d" , 3);                          // prints 3
  if(3>1)
  {
    // count(3-1)
    {
      print("%d" , 2);                      // prints 2
      if(2>1)
      {
        count(2-1);
      }
      print("Integer value is %d\n" , 2);   // prints 2
      return 2;
    }
  }
  print("Integer value is %d\n" , 3);       // prints 3
  return 3;
}
再次:

int count(3) // call with 3
{
  print("%d" , 3);                          // prints 3
  if(3>1)
  {
    // count(3-1)
    {
      print("%d" , 2);                      // prints 2
      if(2>1)
      {
        // count(2-1);
        {
          print("%d" , 1);                      // prints 1
          if(1>1)                            // is false, no recursion any more
          {
            // count(1-1);
          }
          print("Integer value is %d\n" , 1);   // prints 1
          return 1;
        }
      }
      print("Integer value is %d\n" , 2);   // prints 2
      return 2;
    }
  }
  print("Integer value is %d\n" , 3);       // prints 3
  return 3;
}

它打印了6次。这是,因为节目是这么说的。让我们在主代码中插入函数调用:

int count(3) // call with 3
{
  print("%d" , 3);                      // prints 3
  if(3>1)
  {
    count(3-1);
  }
  print("Integer value is %d\n" , n);   // prints 3
  return n;
}
再次插入呼叫:

int count(3) // call with 3
{
  print("%d" , 3);                          // prints 3
  if(3>1)
  {
    // count(3-1)
    {
      print("%d" , 2);                      // prints 2
      if(2>1)
      {
        count(2-1);
      }
      print("Integer value is %d\n" , 2);   // prints 2
      return 2;
    }
  }
  print("Integer value is %d\n" , 3);       // prints 3
  return 3;
}
再次:

int count(3) // call with 3
{
  print("%d" , 3);                          // prints 3
  if(3>1)
  {
    // count(3-1)
    {
      print("%d" , 2);                      // prints 2
      if(2>1)
      {
        // count(2-1);
        {
          print("%d" , 1);                      // prints 1
          if(1>1)                            // is false, no recursion any more
          {
            // count(1-1);
          }
          print("Integer value is %d\n" , 1);   // prints 1
          return 1;
        }
      }
      print("Integer value is %d\n" , 2);   // prints 2
      return 2;
    }
  }
  print("Integer value is %d\n" , 3);       // prints 3
  return 3;
}

因为您正在递归调用函数count()。所以count是3,这个程序将生成3个堆栈帧(count将像3 2 1一样调用自身三次),然后将打印1 2 3。因为您是递归调用函数count()。所以count是3,这个程序将生成3个堆栈帧(count会像3 2 1一样调用自身三次),然后将打印1 2 3。不客气。如果答案正确,请将其标记为正确。不客气。如果答案正确,请将其标记为正确。