5 Replies - 241 Views - Last Post: 06 May 2010 - 02:45 AM Rate Topic: -----

#1 light09  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4462
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 light09  Icon User is offline

  • D.I.C Head

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

Re: Function

Posted 06 May 2010 - 02:34 AM

View Postno2pencil, 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?

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4462
  • View blog
  • 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 :)
Was This Post Helpful? 1
  • +
  • -

#5 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

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

Re: Function

Posted 06 May 2010 - 02:40 AM

View Postlight09, 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 :)
Was This Post Helpful? 1
  • +
  • -

#6 light09  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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 :smile2:



Thank you so much for explaining it, it makes more sense to me now. :bigsmile:

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1