user7738839
user7738839

Reputation: 13

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.

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?

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

Level 2: n location 0x7ffd8988c9dc

Level 3: n location 0x7ffd8988c9bc

Level 4: n location 0x7ffd8988c99c

LEVEL 4: n location 0x7ffd8988c99c

LEVEL 3: n location 0x7ffd8988c9bc

LEVEL 2: n location 0x7ffd8988c9dc

LEVEL 1: n location 0x7ffd8988c9fc

Upvotes: 0

Views: 105

Answers (4)

arminb
arminb

Reputation: 2093

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.

Step 2: Draw a cross onto the piece of paper.

Step 3: Count all crosses and shout out loud: "I have ENTERED room number [count of crosses]".

Step 4.1: If you counted less than or equal 4 crosses, go to Step 1.

Step 4.2: If you counted 4 or more crosses, go to Step 5.

Step 5: Leave the room.

Step 6: Erase one cross from the piece of paper.

Step 7: Count all crosses and shout out loud: "I have LEFT room number [count of crosses]" and go to step 5.

You are finished with the task when you reach the start again.

Upvotes: 0

jfMR
jfMR

Reputation: 24738

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 (i.e.: up_and_down(1)) you have the following context:

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

The rectangle above represents the context inside of the call up_and_down(1).

The second time this function is recursively called (i.e.: up_and_down(2) ), the context is:

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

For the third call (i.e.: up_and_down(3)):

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

And so on until n is equal to 4 (i.e.: 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. Therefore it won't occur any additional recursive call (i.e.: the call up_and_down(5) never takes place). For that reason n == 4 is said to be the exit condition for this recursive function.

The execution flow continues with the second printf() (i.e.: LEVEL 4 is displayed). 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) (i.e.: LEVEL 3 is this time displayed).

All the following steps occur analogously.

Upvotes: 1

Kniwx
Kniwx

Reputation: 32

#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 */
}

Upvotes: 0

RomMer
RomMer

Reputation: 1069

#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 value is 1 and 1 < 4. Then it matches the condition and the function calls itself using n + 1 as parameter; it will run the function again but with 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)

as you can see up_and_down is called IN up_and_down function

Upvotes: 0

Related Questions