Hey i m reading tutrial about rucursion and got code whish isnt complicated or anything but i dunno why it prints the way it does

/* recur.c -- recursion illustration */

#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); /* 1 */

    if (n < 4)

         up_and_down(n+1);

    printf("LEVEL %d: n location %p\n", n, &n); /* 2 */



}

here it prints
Level 1: n location 0022FF60
Level 2: n location 0022FF40
Level 3: n location 0022FF20
Level 4: n location 0022FF00
LEVEL 4: n location 0022FF00
LEVEL 3: n location 0022FF20
LEVEL 2: n location 0022FF40
LEVEL 1: n location 0022FF60


first i know why it prints from 1 to 3 but see here it keeps calling itself till number is smaller than 4 right then after it finishes it goes back to that printf after that if statement whish should print the normal i whish is 1 but it doesnt it prints 4 4 3 2 1 why it got decremented there nowhere in the code where it said i-1 even when the function called itself even if we alrdy specified i as 1 and function calling itself somehow decremented it should when i==3 that if statement will terminate whish will print then orginal i ?

Recommended Answers

All 5 Replies

At the tip, you've got

up_and_down(1)
  up_and_down(2)
    up_and_down(3)
      up_and_down(4)

There is no subtraction, just a nested set of values stored as local parameter 'n'.
As you return from each recursive call, you get back the previous value of n.

oh so it keep calling function till ruc level 4 then it goes to other function whish level 3 then level 2 then level 1 again whish is the main one ?

oh thanks if only the book would put it in that simple

Hey i m reading tutrial about rucursion and got code whish isnt complicated or anything but i dunno why it prints the way it does

/* recur.c -- recursion illustration */

#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); /* 1 */

    if (n < 4)

         up_and_down(n+1);

    printf("LEVEL %d: n location %p\n", n, &n); /* 2 */



}

here it prints
Level 1: n location 0022FF60
Level 2: n location 0022FF40
Level 3: n location 0022FF20
Level 4: n location 0022FF00
LEVEL 4: n location 0022FF00
LEVEL 3: n location 0022FF20
LEVEL 2: n location 0022FF40
LEVEL 1: n location 0022FF60


first i know why it prints from 1 to 3 but see here it keeps calling itself till number is smaller than 4 right then after it finishes it goes back to that printf after that if statement whish should print the normal i whish is 1 but it doesnt it prints 4 4 3 2 1 why it got decremented there nowhere in the code where it said i-1 even when the function called itself even if we alrdy specified i as 1 and function calling itself somehow decremented it should when i==3 that if statement will terminate whish will print then orginal i ?

reply:as the last printf stement s the part of the function up n down it will be executed as many times as the function is called,but before the statement is executed the function is called again and again untill n becomes4. now when the func is excuted for the last time it executes the last printf statement and returns to its calling function to execute it's last printf statement.hence the pattern 4 3 2 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.