1

When I excuted this belo program, it is printing 5 inifnitely. Why? Is it because the decrement is not happening or before decrement happens function call is happening?

I have tried the alternate way making fun(--n), it gave me correct answer. But why it is not working for fun(n--)?

void fun(int n)
{
    if(!n)
    {
        cout << n << " " << endl;
    }
    else
    {
        cout << n << " "<<endl;
        fun(n--);
    }
}

int main()
{
    int n = 5;
    fun(n);
system("pause");
return 0;
}
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122

7 Answers7

7

you need to do foo(--n) and not foo(n--)

  • --n will decrement the value of n, and then send the decremented value
  • n-- will decrement the value of n, but send the pre-decremented value.

so when you do foo(n--) you decement the value of n, but send to the foo function the n bofore decrementing. as you can guess that will go forever

void fun(int n)
{
    if(!n)
    {
        cout << n << " " << endl;
    }
    else
    {
        cout << n << " "<<endl;
        fun(--n);
    }
}

int main()
{
    int n = 5;
    fun(n);
system("pause");
return 0;
}

to learn more on the difference between n-- and --n read here

Community
  • 1
  • 1
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • It does not decrement n *after* the function has ended. its like `auto tmp = n; --n; fun(tmp);` and not like `fun(n); --n;` – Arne Mertz Aug 14 '13 at 07:51
3

Because fun(n--); means callfun with value n and then decrement n.

Marc Claesen
  • 16,778
  • 6
  • 27
  • 62
3

Because n-- returns n before decrementing its value. Since you are calling your function that way, n always comes with the same value. You could write func(n--) that way :

int temp = n;
n = n - 1;
func(temp);
Nbr44
  • 2,042
  • 13
  • 15
  • It does not decrement n after the function has ended, but n-- returns the value n had before the increment was done. its like `auto tmp = n; --n; fun(tmp);` and not like `fun(n); --n;` – Arne Mertz Aug 14 '13 at 07:46
  • 1
    I see, this does make sense. I'll modify my answer accordingly. Thanks ! – Nbr44 Aug 14 '13 at 07:48
3

Note that foo( n-- ) will return n and then decrement n by one (see this), hence returning 5, 5, 5, ... repeated. You need to do one of the following:

foo( --n ); // or,
foo( n - 1 );

... and thus your code should look like this:

void fun( int n ) {
  if( !n ) {
    cout << n << " " << endl;
  } else {
    cout << n << " "<< endl;

    n--;
    fun( n );
  }
}

int main( void ) {
  int n = 5;

  fun( n );
  system("pause");

  return 0;
}

Aside: It's good practice to not include any increment or decrement operations within another expression. Had you of done the following it would of been ok:

n--;
foo( n );

... it can lead to confusion as a client (and even as the programmer) if you begin incrementing and decrementing within expressions. Consider this as another example where doing so is a bad idea:

if ( ( condition_1 == true ) && ( i++ == val ) )

... if the first condition is false it will never reach the second condition and hence not increment i.

Jacob Pollack
  • 3,703
  • 1
  • 17
  • 39
2

Use fun(--n) instead of fun(n--)

The reason this happens this way is because n-- decrements after the function has been run returning 5 repeatedly while --n decrements before.

2

try :

void fun(int n)
{
    if(!n)
    {
        cout << n << " " << endl;
    }
    else
    {
        cout << n << " "<<endl;
        fun(--n);
    }
}
  • The OP already stated in his question that he knows that this works. He wanted to know why. – Hulk Aug 14 '13 at 07:56
1

Your supplying the same value to fun() as was supplied in previous call.

user2672165
  • 2,986
  • 19
  • 27