## C and C++

Moderators: None (Apply to moderate this forum)
Number of posts: 94611

This Forum Only

Recursive Function Posted by tokoG on 8 May 2006 at 9:10 PM
Hi

I am bit confused about the recuersive function details.
Pls see the code below it's pretty simple.
And here is my understanding of what's happening inside but I know it's wrong. Could anyone point where my understanding went wrong?

If...
sum(3) : Goes into the recursive function
with the parameter of sum(3-1 = 2)
sum(2) : Goes into the recursive function
with the parameter of sum(2-1 = 1)
sum(1) : Goes into the if(n==1) and return 1

After the return 1, the function recurse.

Question:Does 1 of return 1 have any numeric value?

Recursion:
n = 1
sum(n-1) = 0
n + (n-1) = 0[/red]

n = 2
sum(n-1) = 1
n + (n-1) = 3[/red]

n = 3
sum(n-1) = 2
n + (n-1) = 5[/red]

This logic should NOT output 6 as an answer but it does, so my understanding above should be incorrect.

Could anyone point me out please?
Thank you!!

Here is the code & output;
```#include <stdio.h>

int sum(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
else
return n + sum(n - 1);

}

void main() {
int result;

result = sum(3);
printf("sum(3) is %d\n", result);

result = sum(10);
printf("sum(10) is %d\n", result);

getchar();
}
```

OUTPUT
Sum(3) is 6
Sum(10) is 55
Re: Recursive Function Posted by Donotalo on 8 May 2006 at 10:02 PM
: Hi
:
: I am bit confused about the recuersive function details.
: Pls see the code below it's pretty simple.
: And here is my understanding of what's happening inside but I know it's wrong. Could anyone point where my understanding went wrong?
:
: If...
: sum(3) : Goes into the recursive function
: with the parameter of sum(3-1 = 2)
: sum(2) : Goes into the recursive function
: with the parameter of sum(2-1 = 1)
: sum(1) : Goes into the if(n==1) and return 1
:
: After the return 1, the function recurse.
:
: Question:Does 1 of return 1 have any numeric value?
:
: Recursion:
: n = 1
: sum(n-1) = 0
: n + (n-1) = 0[/red]
:
: n = 2
: sum(n-1) = 1
: n + (n-1) = 3[/red]
:
: n = 3
: sum(n-1) = 2
: n + (n-1) = 5[/red]
:
:
: This logic should NOT output 6 as an answer but it does, so my understanding above should be incorrect.
:
: Could anyone point me out please?
: Thank you!!
:
: Here is the code & output;
:
```: #include <stdio.h>
:
: int sum(int n) {
:   if (n == 0)
:     return 0;
:   if (n == 1)
:     return 1;
:   else
:     return n + sum(n - 1);
:
: }
:
: void main() {
:   int result;
:
:   result = sum(3);
:   printf("sum(3) is %d\n", result);
:
:   result = sum(10);
:   printf("sum(10) is %d\n", result);
:
:   getchar();
: }
: ```

:
: OUTPUT
: Sum(3) is 6
: Sum(10) is 55
:

Hey tokoG, u r almost got it
here i am explaining again sum(3) in my way:
```
sum(3)
n = 3
return 3 + sum(3-1)
sum(2)
n = 2
return 2 + sum(2-1)
sum(1)
n = 1
return 1
return 2 + 1
return 3 + 3
```

and the last return statement exits from the call sum(3) returning 6.

~Donotalo()

Re: Recursive Function Posted by tokoG on 9 May 2006 at 8:53 PM
This message was edited by tokoG at 2006-5-9 20:54:39

:
: Hey tokoG, u r almost got it
: here i am explaining again sum(3) in my way:
:
```
: sum(3)
:     n = 3
:     return 3 + sum(3-1)
:         sum(2)
:             n = 2
:             return 2 + sum(2-1)
:                 sum(1)
:                     n = 1
:                     return 1
:             return 2 + 1
:     return 3 + 3
: ```

: and the last return statement exits from the call sum(3) returning 6.
:

:
~Donotalo()
:
:

Hi Donotalo, how are you?

Thanks for the reply, does it mean when...

```:                 sum(1)
:                     n = 1
:                     return 1
```

The n = becomes 2 [(n=1) + (return 1) = 2]at the sentence...

```return n(2) + (n-1)1
```

is it?
Re: Recursive Function Posted by Donotalo on 10 May 2006 at 10:53 AM
: This message was edited by tokoG at 2006-5-9 20:54:39

: :
: : Hey tokoG, u r almost got it
: : here i am explaining again sum(3) in my way:
: :
```
: : sum(3)
: :     n = 3
: :     return 3 + sum(3-1)
: :         sum(2)
: :             n = 2
: :             return 2 + sum(2-1)
: :                 sum(1)
: :                     n = 1
: :                     return 1
: :             return 2 + 1
: :     return 3 + 3
: : ```

: : and the last return statement exits from the call sum(3) returning 6.
: :

: :
~Donotalo()
: :
: :
:
: Hi Donotalo, how are you?
:
: Thanks for the reply, does it mean when...
:
:
```: :                 sum(1)
: :                     n = 1
: :                     return 1
: ```

:
: The n = becomes 2 [(n=1) + (return 1) = 2]at the sentence...
:
:
```: return n(2) + (n-1)1
: ```

:
: is it?
:
hey, i'm fine.

probably u still have some confusion. inside sum(1), n equals to 1. inside sum(2), n equals to 2... as u know from functions - these are their local parameters.

well, if sum(2) [n = 2] has been called, it will return n + sum(1). but where it will find the value of sum(1)? well, it will call the function sum with the parameter 1. then, two copies of the function sum() will remain inside RAM - one is for the call sum(2) and the other is for the call sum(1).

when sum(2) is looking for the value of sum(1), sum(2) hasn't finished its execution. even when sum(2) is "evaluating" sum(1) by calling "itself," sum(2) is still in the memory and hasn't finished its execution. so in this sum(2) sum(1) case, there are two copies of n in memory, one is holding the value 2, the other is 1, for sum(2) and sum(1) respectively.

sum(1) returns 1, causing sum(2) to return 2 + sum(1) which is 2 + 1. so no change of n inside sum(2).

now guess what will happen in the call sum(100). sum(100) will call sum(99), sum(99) will call sum(98), ..., sum(2) will call sum(1), sum(1) will return 1. when sum(1) starts execution, there are 100 copies of sum inside RAM and thus 100 copies of n are there to hold the values 100 through 1.

i'm sleepy, so there might be mistakes in my explanation. forgive me for that

~Donotalo()

Re: Recursive Function Posted by tokoG on 10 May 2006 at 8:36 PM
: hey, i'm fine.
:
: probably u still have some confusion. inside sum(1), n equals to 1. inside sum(2), n equals to 2... as u know from functions - these are their local parameters.
:
: well, if sum(2) [n = 2] has been called, it will return n + sum(1). but where it will find the value of sum(1)? well, it will call the function sum with the parameter 1. then, two copies of the function sum() will remain inside RAM - one is for the call sum(2) and the other is for the call sum(1).
:
: when sum(2) is looking for the value of sum(1), sum(2) hasn't finished its execution. even when sum(2) is "evaluating" sum(1) by calling "itself," sum(2) is still in the memory and hasn't finished its execution. so in this sum(2) sum(1) case, there are two copies of n in memory, one is holding the value 2, the other is 1, for sum(2) and sum(1) respectively.
:
: sum(1) returns 1, causing sum(2) to return 2 + sum(1) which is 2 + 1. so no change of n inside sum(2).
:
: now guess what will happen in the call sum(100). sum(100) will call sum(99), sum(99) will call sum(98), ..., sum(2) will call sum(1), sum(1) will return 1. when sum(1) starts execution, there are 100 copies of sum inside RAM and thus 100 copies of n are there to hold the values 100 through 1.
:
: i'm sleepy, so there might be mistakes in my explanation. forgive me for that
:

:
~Donotalo()
:
:

Hi Donotalo

Brilliant!! Now I got it completely!!
Great explanation. Now I knew where I was misunderstanding was that the statement of,

return n + sum(n-1)

sum(n-1) is INDEED the another function (that's why it's a recursive function) to be called and to return it's own local value.

At here,

return n + sum(n-1)

Each function is returning it's own different local value which is also the parameter of each different functions.

Thanks for clarifying my misunderstanding.
This will make my further study far more interesting and easy. :)

Cheers!

PS: Hope you will get a good sleep. ;)
Re: Recursive Function Posted by Donotalo on 10 May 2006 at 10:10 PM
: : hey, i'm fine.
: :
: : probably u still have some confusion. inside sum(1), n equals to 1. inside sum(2), n equals to 2... as u know from functions - these are their local parameters.
: :
: : well, if sum(2) [n = 2] has been called, it will return n + sum(1). but where it will find the value of sum(1)? well, it will call the function sum with the parameter 1. then, two copies of the function sum() will remain inside RAM - one is for the call sum(2) and the other is for the call sum(1).
: :
: : when sum(2) is looking for the value of sum(1), sum(2) hasn't finished its execution. even when sum(2) is "evaluating" sum(1) by calling "itself," sum(2) is still in the memory and hasn't finished its execution. so in this sum(2) sum(1) case, there are two copies of n in memory, one is holding the value 2, the other is 1, for sum(2) and sum(1) respectively.
: :
: : sum(1) returns 1, causing sum(2) to return 2 + sum(1) which is 2 + 1. so no change of n inside sum(2).
: :
: : now guess what will happen in the call sum(100). sum(100) will call sum(99), sum(99) will call sum(98), ..., sum(2) will call sum(1), sum(1) will return 1. when sum(1) starts execution, there are 100 copies of sum inside RAM and thus 100 copies of n are there to hold the values 100 through 1.
: :
: : i'm sleepy, so there might be mistakes in my explanation. forgive me for that
: :

: :
~Donotalo()
: :
: :
:
: Hi Donotalo
:
: Brilliant!! Now I got it completely!!
: Great explanation. Now I knew where I was misunderstanding was that the statement of,
:
: return n + sum(n-1)
:
: sum(n-1) is INDEED the another function (that's why it's a recursive function) to be called and to return it's own local value.
:
: At here,
:
: return n + sum(n-1)
:
: Each function is returning it's own different local value which is also the parameter of each different functions.
:
: Thanks for clarifying my misunderstanding.
: This will make my further study far more interesting and easy. :)
:
:
: Cheers!
:
: PS: Hope you will get a good sleep. ;)
:

but tokoG, one precaution u shud take while programming a recursive function. sometimes it is easy to think recursively and thus it is easy to design a recursive function. but a recursive function may take much more memory than its iterative version. so a recursive function may exhaust memory. though memory shud not be a concern in today's computers, u shud be careful while designing a recursive function in professional life.

~Donotalo()

Re: Recursive Function Posted by tokoG on 13 May 2006 at 11:29 PM
: : : hey, i'm fine.
: : :
: : : probably u still have some confusion. inside sum(1), n equals to 1. inside sum(2), n equals to 2... as u know from functions - these are their local parameters.
: : :
: : : well, if sum(2) [n = 2] has been called, it will return n + sum(1). but where it will find the value of sum(1)? well, it will call the function sum with the parameter 1. then, two copies of the function sum() will remain inside RAM - one is for the call sum(2) and the other is for the call sum(1).
: : :
: : : when sum(2) is looking for the value of sum(1), sum(2) hasn't finished its execution. even when sum(2) is "evaluating" sum(1) by calling "itself," sum(2) is still in the memory and hasn't finished its execution. so in this sum(2) sum(1) case, there are two copies of n in memory, one is holding the value 2, the other is 1, for sum(2) and sum(1) respectively.
: : :
: : : sum(1) returns 1, causing sum(2) to return 2 + sum(1) which is 2 + 1. so no change of n inside sum(2).
: : :
: : : now guess what will happen in the call sum(100). sum(100) will call sum(99), sum(99) will call sum(98), ..., sum(2) will call sum(1), sum(1) will return 1. when sum(1) starts execution, there are 100 copies of sum inside RAM and thus 100 copies of n are there to hold the values 100 through 1.
: : :
: : : i'm sleepy, so there might be mistakes in my explanation. forgive me for that
: : :

: : :
~Donotalo()
: : :
: : :
: :
: : Hi Donotalo
: :
: : Brilliant!! Now I got it completely!!
: : Great explanation. Now I knew where I was misunderstanding was that the statement of,
: :
: : return n + sum(n-1)
: :
: : sum(n-1) is INDEED the another function (that's why it's a recursive function) to be called and to return it's own local value.
: :
: : At here,
: :
: : return n + sum(n-1)
: :
: : Each function is returning it's own different local value which is also the parameter of each different functions.
: :
: : Thanks for clarifying my misunderstanding.
: : This will make my further study far more interesting and easy. :)
: :
: :
: : Cheers!
: :
: : PS: Hope you will get a good sleep. ;)
: :
:
: but tokoG, one precaution u shud take while programming a recursive function. sometimes it is easy to think recursively and thus it is easy to design a recursive function. but a recursive function may take much more memory than its iterative version. so a recursive function may exhaust memory. though memory shud not be a concern in today's computers, u shud be careful while designing a recursive function in professional life.
:

:
~Donotalo()
:
:

Hi

Yes, my tutorial book says that's why the recursive takes alot of space. ;)

There is another recursive code that I can't quite work out.
It's Fibonacci consequences.

In here, I think the i=0 --return-->0 and i=1 --return-->1 happens right away from the programme execution, no recursive. But when i=2, the recursive occur but how does it happen?

The following is my idea and this calculation is OK until i=3.
But not from i=4. So I think this is wrong.

i=2 .....Output is 1
return fib(n-1)+ fib(n-2)

(2-1=1) + (2-2=0)
return 1+0

i=3 .....Output is 2
return fib(n-1)+ fib(n-2)

(3-1=2)
(2-1=1)
return 1

+

fib(n-2)
(3-2=1)
return 1

```#include <stdio.h>

int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else {
return fibonacci(n-1) + fibonacci(n-2);
}
}

void main() {
int i;

printf("The first 10 numbers in the Fibonacci series\n");
for (i=0; i<10; i++)
printf("%d ", fibonacci(i));

getchar();

}
```

Re: Recursive Function Posted by IDK on 14 May 2006 at 3:42 AM
: : : : hey, i'm fine.
: : : :
: : : : probably u still have some confusion. inside sum(1), n equals to 1. inside sum(2), n equals to 2... as u know from functions - these are their local parameters.
: : : :
: : : : well, if sum(2) [n = 2] has been called, it will return n + sum(1). but where it will find the value of sum(1)? well, it will call the function sum with the parameter 1. then, two copies of the function sum() will remain inside RAM - one is for the call sum(2) and the other is for the call sum(1).
: : : :
: : : : when sum(2) is looking for the value of sum(1), sum(2) hasn't finished its execution. even when sum(2) is "evaluating" sum(1) by calling "itself," sum(2) is still in the memory and hasn't finished its execution. so in this sum(2) sum(1) case, there are two copies of n in memory, one is holding the value 2, the other is 1, for sum(2) and sum(1) respectively.
: : : :
: : : : sum(1) returns 1, causing sum(2) to return 2 + sum(1) which is 2 + 1. so no change of n inside sum(2).
: : : :
: : : : now guess what will happen in the call sum(100). sum(100) will call sum(99), sum(99) will call sum(98), ..., sum(2) will call sum(1), sum(1) will return 1. when sum(1) starts execution, there are 100 copies of sum inside RAM and thus 100 copies of n are there to hold the values 100 through 1.
: : : :
: : : : i'm sleepy, so there might be mistakes in my explanation. forgive me for that
: : : :

: : : :
~Donotalo()
: : : :
: : : :
: : :
: : : Hi Donotalo
: : :
: : : Brilliant!! Now I got it completely!!
: : : Great explanation. Now I knew where I was misunderstanding was that the statement of,
: : :
: : : return n + sum(n-1)
: : :
: : : sum(n-1) is INDEED the another function (that's why it's a recursive function) to be called and to return it's own local value.
: : :
: : : At here,
: : :
: : : return n + sum(n-1)
: : :
: : : Each function is returning it's own different local value which is also the parameter of each different functions.
: : :
: : : Thanks for clarifying my misunderstanding.
: : : This will make my further study far more interesting and easy. :)
: : :
: : :
: : : Cheers!
: : :
: : : PS: Hope you will get a good sleep. ;)
: : :
: :
: : but tokoG, one precaution u shud take while programming a recursive function. sometimes it is easy to think recursively and thus it is easy to design a recursive function. but a recursive function may take much more memory than its iterative version. so a recursive function may exhaust memory. though memory shud not be a concern in today's computers, u shud be careful while designing a recursive function in professional life.
: :

: :
~Donotalo()
: :
: :
:
: Hi
:
: Yes, my tutorial book says that's why the recursive takes alot of space. ;)
:
: There is another recursive code that I can't quite work out.
: It's Fibonacci consequences.
:
: In here, I think the i=0 --return-->0 and i=1 --return-->1 happens right away from the programme execution, no recursive. But when i=2, the recursive occur but how does it happen?
:
: The following is my idea and this calculation is OK until i=3.
: But not from i=4. So I think this is wrong.
:
: i=2 .....Output is 1
: return fib(n-1)+ fib(n-2)
:
: (2-1=1) + (2-2=0)
: return 1+0
:
: i=3 .....Output is 2
: return fib(n-1)+ fib(n-2)
:
: (3-1=2)
: (2-1=1)
: return 1
:
: +
:
: fib(n-2)
: (3-2=1)
: return 1
:
:
:
```: #include <stdio.h>
:
: int fibonacci(int n) {
:   if (n == 0)
:     return 0;
:   else if (n == 1)
:     return 1;
:   else {
:     return fibonacci(n-1) + fibonacci(n-2);
:   }
: }
:
: void main() {
:   int i;
:
:   printf("The first 10 numbers in the Fibonacci series\n");
:   for (i=0; i<10; i++)
:     printf("%d ", fibonacci(i));
:
:   getchar();
:
: }
: ```

:
That's right, the fibonacci sequence is like this (with indexes under):
0 1 1 2 3 5 8 13 ...
0 1 2 3 4 5 6 7

Where's the err?
Re: Recursive Function Posted by tokoG on 14 May 2006 at 6:24 PM
: : Hi
: :
: : Yes, my tutorial book says that's why the recursive takes alot of space. ;)
: :
: : There is another recursive code that I can't quite work out.
: : It's Fibonacci consequences.
: :
: : In here, I think the i=0 --return-->0 and i=1 --return-->1 happens right away from the programme execution, no recursive. But when i=2, the recursive occur but how does it happen?
: :
: : The following is my idea and this calculation is OK until i=3.
: : But not from i=4. So I think this is wrong.
: :
: : i=2 .....Output is 1
: : return fib(n-1)+ fib(n-2)
: :
: : (2-1=1) + (2-2=0)
: : return 1+0
: :
: : i=3 .....Output is 2
: : return fib(n-1)+ fib(n-2)
: :
: : (3-1=2)
: : (2-1=1)
: : return 1
: :
: : +
: :
: : fib(n-2)
: : (3-2=1)
: : return 1
: :
: :
: :
```: : #include <stdio.h>
: :
: : int fibonacci(int n) {
: :   if (n == 0)
: :     return 0;
: :   else if (n == 1)
: :     return 1;
: :   else {
: :     return fibonacci(n-1) + fibonacci(n-2);
: :   }
: : }
: :
: : void main() {
: :   int i;
: :
: :   printf("The first 10 numbers in the Fibonacci series\n");
: :   for (i=0; i<10; i++)
: :     printf("%d ", fibonacci(i));
: :
: :   getchar();
: :
: : }
: : ```

: :
: That's right, the fibonacci sequence is like this (with indexes under):
: 0 1 1 2 3 5 8 13 ...
: 0 1 2 3 4 5 6 7
:
: Where's the err?
:

Hi

My quetion is from when i=4.

i=4 .....Output should be 3
return fib(n-1)+ fib(n-2)

(4-1=3)
(3-1=2)
(2-1=1)
return 1
Is this (Returning 1) the really the case.....?

+

fib(n-2)
(4-2=2)
(2-2=0)
return 0
Is this (Returning 0) the really the case.....?

I don't understand how (n-1)+(n-2) calculation works in here...
Re: Recursive Function Posted by Donotalo on 14 May 2006 at 10:37 PM
: : : Hi
: : :
: : : Yes, my tutorial book says that's why the recursive takes alot of space. ;)
: : :
: : : There is another recursive code that I can't quite work out.
: : : It's Fibonacci consequences.
: : :
: : : In here, I think the i=0 --return-->0 and i=1 --return-->1 happens right away from the programme execution, no recursive. But when i=2, the recursive occur but how does it happen?
: : :
: : : The following is my idea and this calculation is OK until i=3.
: : : But not from i=4. So I think this is wrong.
: : :
: : : i=2 .....Output is 1
: : : return fib(n-1)+ fib(n-2)
: : :
: : : (2-1=1) + (2-2=0)
: : : return 1+0
: : :
: : : i=3 .....Output is 2
: : : return fib(n-1)+ fib(n-2)
: : :
: : : (3-1=2)
: : : (2-1=1)
: : : return 1
: : :
: : : +
: : :
: : : fib(n-2)
: : : (3-2=1)
: : : return 1
: : :
: : :
: : :
```: : : #include <stdio.h>
: : :
: : : int fibonacci(int n) {
: : :   if (n == 0)
: : :     return 0;
: : :   else if (n == 1)
: : :     return 1;
: : :   else {
: : :     return fibonacci(n-1) + fibonacci(n-2);
: : :   }
: : : }
: : :
: : : void main() {
: : :   int i;
: : :
: : :   printf("The first 10 numbers in the Fibonacci series\n");
: : :   for (i=0; i<10; i++)
: : :     printf("%d ", fibonacci(i));
: : :
: : :   getchar();
: : :
: : : }
: : : ```

: : :
: : That's right, the fibonacci sequence is like this (with indexes under):
: : 0 1 1 2 3 5 8 13 ...
: : 0 1 2 3 4 5 6 7
: :
: : Where's the err?
: :
:
: Hi
:
: My quetion is from when i=4.
:
: i=4 .....Output should be 3
: return fib(n-1)+ fib(n-2)
:
: (4-1=3)
: (3-1=2)
: (2-1=1)
: return 1
: Is this (Returning 1) the really the case.....?
:
: +
:
: fib(n-2)
: (4-2=2)
: (2-2=0)
: return 0
: Is this (Returning 0) the really the case.....?
:
: I don't understand how (n-1)+(n-2) calculation works in here...
:
think recursively!
u got right when i is in [1, 3]. so, what will fibonacci(4) return? it will return:

fibonacci(3) + fibonacci(2)

u've already calculated those values put them and sum them up, u will got the answer.

here both fibonacci(3) and fibonacci(2) are separate functions and take separate portions of the memory.

ok, let me browse through it:
```fibo(4)
return fibo(3) + fibo(2)
fibo(3)
return fibo(2) + fibo(1)
fibo(2)
return fibo(1) + fibo(0)
fibo(1)
return 1
fibo(0)
return 0
fibo(2) = 1
fibo(1)
return 1
fibo(1) = 1
fibo(3) = 2
fibo(2)
return fibo(1) + fibo(0)
fibo(1)
return 1
fibo(0)
return 0
fibo(2) = 1
fibo(4) = 3
```

~Donotalo()