3

Here is a recursive function -

private void printVar01(int t){
    if(t != 0){
        logp.info("o: " + t);
        printVar01(t-1);
    }
}

The same function with a slight modification -

private void printVar02(int t){
    if(t != 0){
        logp.info("o: " + t);
        printVar02(t--);
    }
}

If I pass in a integer value, like 4, printVar01 works as expected, where t decrements to 0 in successive recursive calls, eventually causing the program to exit.

With printVar02, t stays at value 4.

Why? I am assuming this has something to do with variable assignment and/or how values are passed to functions.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Quest Monger
  • 8,252
  • 11
  • 37
  • 43

3 Answers3

3

t-1 does not change t while t-- does.

  • t-1 gives you a new value without affecting the actual value of t
  • t-- gives you t and then decreases the value of t

I think printVar02 should work fine and in printVar01 the value remains the same.


For the comment of DoubleMa

actually 01 will works not 02, in 01 t is changing, but in 02 the function is calling itself before changing the value of T, he just need to use --t;

I also definitely suspect the recursive call.

If you mean printVar = printVar01 = printVar02.

If you are calling printVar recursively then t-1 will work as a recursive call. It will make it work and in t-- it will pass the same value, 4, every time as it's a postdecrement. Use predecrement, --t, instead.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
akash
  • 22,664
  • 11
  • 59
  • 87
  • 2
    actually 01 will works not 02, in 01 t is changing, but in 02 the function is calling itself before changing the value of T, he just need to use --t; – DoubleMa May 09 '15 at 03:42
2

Just use --t instead of t--:

private static void printVar02(int t){
if(t != 0){
    logp.info("o: " + t);
    printVar02(--t);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DoubleMa
  • 368
  • 1
  • 8
  • Yes this is right if method name is `printVar` and not `printVar02`. – akash May 09 '15 at 03:47
  • 1
    that what he meant, i got that when he said "Here is a recursive funtion" i'm sure he was just testing it that why he used 01 and 02 – DoubleMa May 09 '15 at 03:49
  • ***Why*** should you use --t instead of t--? What is the explanation? What is the idea/gist? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/30135687/edit), not here in comments (****** ***without*** ****** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Nov 25 '22 at 13:58
2

Post decrement decreases the value by 1 after execution of the statement printVar(t--). Hence each time 4 is being passed to the function.

You should use --t instead which does the decrement first.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raja
  • 851
  • 9
  • 22