int calculate ( int a)
{
if ( a <= 1)
return 1;
else
return ( calculate ( a - 2) -1 );
}
#include <iostream>
using namespace std;
int main ()
{
int n = 6;
cout << calculate(n);
return 0;
}
Function
Page 1 of 15 Replies - 241 Views - Last Post: 06 May 2010 - 02:45 AM
#1
Function
Posted 06 May 2010 - 02:10 AM
Hi there, i have compiled this code, it runs but i am curious to know why the output is negative 2, could someone please explain?
Replies To: Function
#2
Re: Function
Posted 06 May 2010 - 02:21 AM
You are calling calculate from within itself. To see how it got this number run this code:
When I run the above code, it shows the following. The loop is going to run until it's less than 1.
#include <iostream>
using namespace std;
int calculate ( int a) {
printf("Passed %i : \n",a);
if ( a <= 1)
return 1;
else
return ( calculate ( a - 2) -1 );
}
int main () {
int n = 6;
cout << calculate(n);
return 0;
}
When I run the above code, it shows the following. The loop is going to run until it's less than 1.
Quote
>$./calculate
Passed 6 :
Passed 4 :
Passed 2 :
Passed 0 :
-2
Passed 6 :
Passed 4 :
Passed 2 :
Passed 0 :
-2
#3
Re: Function
Posted 06 May 2010 - 02:34 AM
no2pencil, on 06 May 2010 - 01:21 AM, said:
You are calling calculate from within itself. To see how it got this number run this code:
When I run the above code, it shows the following. The loop is going to run until it's less than 1.
#include <iostream>
using namespace std;
int calculate ( int a) {
printf("Passed %i : \n",a);
if ( a <= 1)
return 1;
else
return ( calculate ( a - 2) -1 );
}
int main () {
int n = 6;
cout << calculate(n);
return 0;
}
When I run the above code, it shows the following. The loop is going to run until it's less than 1.
Quote
>$./calculate
Passed 6 :
Passed 4 :
Passed 2 :
Passed 0 :
-2
Passed 6 :
Passed 4 :
Passed 2 :
Passed 0 :
-2
Quote
printf("Passed %i : \n",a)
Is passed% i a pointer? ( calculate ( a - 2) -1 ) in this function ( a- 2) never is subtracted by 1 yes? In a way the placement of -1 is obsolete in the function?
Is passed% i a pointer? ( calculate ( a - 2) -1 ) in this function ( a- 2) never is subtracted by 1 yes? In a way the placement of -1 is obsolete in the function?
#4
Re: Function
Posted 06 May 2010 - 02:36 AM
Can you :
1.) Not quote your own text, especially when it's new
2.) Not quote my entire post.
I'm having a hard time determining what you are posting with everything being quoted. Just type, & click send
1.) Not quote your own text, especially when it's new
2.) Not quote my entire post.
I'm having a hard time determining what you are posting with everything being quoted. Just type, & click send
#5
Re: Function
Posted 06 May 2010 - 02:40 AM
light09, on 06 May 2010 - 01:10 AM, said:
Hi there, i have compiled this code, it runs but i am curious to know why the output is negative 2, could someone please explain?
int calculate ( int a)
{
if ( a <= 1)
return 1;
else
return ( calculate ( a - 2) -1 );
}
#include <iostream>
using namespace std;
int main ()
{
int n = 6;
cout << calculate(n);
return 0;
}
in case of recursive functions, the return values get stored in a runtime stack...
when calculate runs first time, it calls calculate(4) and the '-1' is stored in the stack...
next time it calls calculate(2) and '-1' is again stored on top of the previous -1...
when this happens the third time, calculate(0) returns and -1 is again added to the stack....
now, when the function ends it returns 1(-1-1-1)=-2
i.e. when the function ends all the values in the stack are popped and added to the returned value
hence your ans
#6
Re: Function
Posted 06 May 2010 - 02:45 AM
printf("Passed %i : \n",a)
Is passed% i a pointer? ( calculate ( a - 2) -1 ) in this function ( a- 2) never is subtracted by 1 yes? In a way the placement of -1 is obsolete in the function?
P/S : I really am sorry for the inconvenience caused but i will learn and correct my mistake thank you for pointing it out
Thank you so much for explaining it, it makes more sense to me now.
Is passed% i a pointer? ( calculate ( a - 2) -1 ) in this function ( a- 2) never is subtracted by 1 yes? In a way the placement of -1 is obsolete in the function?
P/S : I really am sorry for the inconvenience caused but i will learn and correct my mistake thank you for pointing it out
Thank you so much for explaining it, it makes more sense to me now.
This post has been edited by light09: 06 May 2010 - 02:47 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|