Function

Page 1 of 1

5 Replies - 241 Views - Last Post: 06 May 2010 - 02:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=172279&amp;s=f6dd4028b49972803ba24d31fb629830&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 light09

Reputation: 0
• Posts: 141
• Joined: 21-November 09

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?

```

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

```

Is This A Good Question/Topic? 0

Replies To: Function

#2 no2pencil

• Original Digital Gansta

Reputation: 4462
• Posts: 24,906
• Joined: 10-May 07

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:

```#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

#3 light09

Reputation: 0
• Posts: 141
• Joined: 21-November 09

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:

```#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

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?

#4 no2pencil

• Original Digital Gansta

Reputation: 4462
• Posts: 24,906
• Joined: 10-May 07

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

#5 aks29921

Reputation: 84
• Posts: 230
• Joined: 24-August 09

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

#6 light09

Reputation: 0
• Posts: 141
• Joined: 21-November 09

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.

This post has been edited by light09: 06 May 2010 - 02:47 AM