简体   繁体   English

不了解此递归功能

[英]Don't understand this recursion function

This is a recursive function from a book i read, since it's not a loop there this function prints those out puts which more like it's looping. 这是我读过的书中的一个递归函数,因为它不是循环,该函数会打印出类似循环的输出。 I have tried to understand how this function up_and_down work. 我试图了解此函数up_and_down工作方式。

In the book i read it say that a recursive function call it self but the in code i can't see it directly how? 在我读过的书中,我说一个递归函数将其称为self,但是在代码中我无法直接看到它呢?

can some one please explain this for step for step what is going on in the function thanks in advance. 可以先谢谢一步,请一步一步解释一下该功能是怎么回事。

#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);
}

output 输出

Level 1: n location 0x7ffd8988c9fc 级别1:n位置0x7ffd8988c9fc

Level 2: n location 0x7ffd8988c9dc 级别2:n个位置0x7ffd8988c9dc

Level 3: n location 0x7ffd8988c9bc 3级:n位置0x7ffd8988c9bc

Level 4: n location 0x7ffd8988c99c 级别4:n位置0x7ffd8988c99c

LEVEL 4: n location 0x7ffd8988c99c 级别4:n位置0x7ffd8988c99c

LEVEL 3: n location 0x7ffd8988c9bc 级别3:n位置0x7ffd8988c9bc

LEVEL 2: n location 0x7ffd8988c9dc 级别2:n个位置0x7ffd8988c9dc

LEVEL 1: n location 0x7ffd8988c9fc 级别1:n位置0x7ffd8988c9fc

Every time the recursive function is called a new context is created and piled up on top of the last context. 每次调用递归函数时,都会创建一个新上下文并将其堆积在最后一个上下文之上。 This context is stored on the stack. 该上下文存储在堆栈中。

At the moment of entering up_and_down() for the first time (ie: up_and_down(1) ) you have the following context: 首次输入up_and_down()时(即: up_and_down(1) ),您具有以下上下文:

-----------
| LEVEL 1 |
-----------

The rectangle above represents the context inside of the call up_and_down(1) . 上方的矩形表示调用up_and_down(1)内部的上下文。

The second time this function is recursively called (ie: up_and_down(2) ), the context is: 第二次递归调用此函数(即: up_and_down(2) ),上下文是:

-----------
| LEVEL 2 |
-----------
-----------
| LEVEL 1 |
-----------

For the third call (ie: up_and_down(3) ): 对于第三个调用(即: up_and_down(3) ):

-----------
| LEVEL 3 |
-----------
-----------
| LEVEL 2 |
-----------
-----------
| LEVEL 1 |
-----------

And so on until n is equal to 4 (ie: up_and_down(4) ): 依此类推,直到n等于4 (即: up_and_down(4) ):

-----------
| LEVEL 4 |
-----------
-----------
| LEVEL 3 |
-----------
-----------
| LEVEL 2 |
-----------
-----------
| LEVEL 1 |
-----------

The if statement in the function up_and_down() is evaluated to false for the first time when n is equal to 4 . n等于4时,函数up_and_down()if语句第一次被评估为false。 Therefore it won't occur any additional recursive call (ie: the call up_and_down(5) never takes place). 因此,它不会发生任何其他递归调用(即,永远不会发生up_and_down(5)调用)。 For that reason n == 4 is said to be the exit condition for this recursive function. 因此, n == 4被认为是该递归函数的退出条件

The execution flow continues with the second printf() (ie: LEVEL 4 is displayed). 执行流程以第二个printf()继续(即:显示LEVEL 4 )。 Then the context of the fourth call is destroyed: 然后,第四个调用的上下文被破坏:

-----------
| LEVEL 3 |
-----------
-----------
| LEVEL 2 |
-----------
-----------
| LEVEL 1 |
-----------

The control flow is the one returning from the call up_and_down(4) , that is, in the recursive function with the context created by the call up_and_down(3) (ie: LEVEL 3 is this time displayed). 控制流程是从调用up_and_down(4)返回的流程,即在具有由调用up_and_down(3)创建的上下文的递归函数中(即,这次显示LEVEL 3 )。

All the following steps occur analogously. 以下所有步骤类似地发生。

#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); // HERE : the function calls itself
  }
  printf("LEVEL %d: n location %p\n",n,&n);
}

the function is called in the main with the an integer as parameter. 该函数在主体中以整数作为参数调用。 When it's called from the main n = 1. then it runs the function (up_and_down), prints the level and the location a first time. 从主n = 1调用它时,它将运行函数(up_and_down),并首次打印级别和位置。

n value is 1 and 1 < 4. Then it matches the condition and the function calls itself using n + 1 as parameter; n值为1且1 <4。然后它匹配条件,并且函数使用n +1作为参数调用自身; it will run the function again but with n = 2. 它将再次运行该函数,但n = 2。

it will do it until n reachs 4 and then the function execution will continue calling the rest of the function (the second printf) 它将一直执行到n达到4为止,然后函数执行将继续调用函数的其余部分(第二个printf)

as you can see up_and_down is called IN up_and_down function 如您所见,up_and_down称为IN up_and_down函数

#include<stdio.h>

void up_and_down(int);
int main(void){
  up_and_down(1);  --> Step 1
  return 0;

}
void up_and_down(int n){

  printf("Level %d: n location %p\n" , n , &n);  --> step 2
  if(n < 4){             /* --> step3 checks if n is less than 4 */

    up_and_down(n+1);   /*--> step 4(if step3 is true) when if condition true this is executed */
  }
  printf("LEVEL %d: n location %p\n",n,&n); /* step5 irrespective of the if condition value of n is printed */
}

Imagine you are standing infront of a door which leads to a room. 想象一下,您正站在通往房间的门前。 The room also has a door inside which leads to the next room and so on. 房间内还有一扇门,通向下一个房间,依此类推。

Also imagine you are holding a pencil and a piece of paper which contains instructions for you to do: 还要想象您拿着一支铅笔和一张纸,其中包含要执行的操作说明:

Step 1: Enter the next room. 步骤1:进入下一个房间。

Step 2: Draw a cross onto the piece of paper. 步骤2:在纸上画一个十字。

Step 3: Count all crosses and shout out loud: "I have ENTERED room number [count of crosses]". 步骤3:计算所有十字架,然后大声喊道:“我输入了房间号[十字架数]”。

Step 4.1: If you counted less than or equal 4 crosses, go to Step 1. 步骤4.1:如果您计算的交叉数少于或等于4,请转到步骤1。

Step 4.2: If you counted 4 or more crosses, go to Step 5. 步骤4.2:如果您计算了4个或更多的十字架,请转到步骤5。

Step 5: Leave the room. 步骤5:离开房间。

Step 6: Erase one cross from the piece of paper. 步骤6:从纸上擦除一个叉。

Step 7: Count all crosses and shout out loud: "I have LEFT room number [count of crosses]" and go to step 5. 步骤7:数所有十字架,然后大声喊出:“我有左房间号[十字架数]”,然后转到步骤5。

You are finished with the task when you reach the start again. 当您再次到达起点时,您就完成了任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM