2

This was my answer to a question in which I was supposed to convert an iterative method to a recursive method. The teacher told me I cant use a-=1 as a parameter... So they gave me 0 points.. When I run this it works as it supposed to be.. Could someone tell me why its wrong?

public int do(int a){

    if(a==0){
        return 1 ;
    }else{
        return a * do(a-=1);
    }
}
elimirks
  • 1,452
  • 2
  • 17
  • 30
tlq
  • 887
  • 4
  • 10
  • 21
  • Please tag your questions with the language you're using, it will help the correct experts find your questions. – Barmar Feb 01 '14 at 01:49

2 Answers2

4

The problem with your code is that you're reading the value of a and reassigning it with a -= 1 in the same expression, but the order of these operations is not specified. The statement:

return a * do(a -= 1);

could be implemented as:

temp = a;
a -= 1;
return temp * do(a);

which will do what you were probably expecting, or:

a -= 1;
return a * do(a);

which will multiply by the decremented value of a rather than its original value.

The correct way to write your function is:

public int do(int a){

    if(a==0){
        return 1 ;
    }else{
        return a * do(a-1);
    }
}

Just pass the result of the subtraction as an argument, don't reassign the variable at the same time.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    While it is true that using the `-=` operator in the example is pointless (hardly a reason to get 0 points), the statement that the order of evaluation is unspecified is false. The code in the question is actually correct according to the JLS. See http://stackoverflow.com/questions/6800590/what-are-the-rules-for-evaluation-order-in-java – Feuermurmel Feb 01 '14 at 01:57
  • Nonetheless, it APPEARS to be quite ambiguous. – elimirks Feb 01 '14 at 01:59
  • Well, he didn't tag the question with the language. Many languages adopt the C rule that this is undefined, and I assumed this was one of those languages. – Barmar Feb 01 '14 at 01:59
  • I'm not a Java programmer, and didn't know it was an exception to that rule. – Barmar Feb 01 '14 at 02:00
  • Finally, even though it may be well defined, it demonstrates a misunderstanding of how recursion is done. Perhaps 0 points is too harsh, but it should get a poor grade. – Barmar Feb 01 '14 at 02:01
  • @Barmar you are right, this is different in other languages. I assume the question is about Java, as it is common to do assignments in a common language like Java. – Feuermurmel Feb 01 '14 at 02:02
  • @Barmar IMHO, f the assignemt was about recursion, the assignment has ben completed correctly. But I agree that using `-=` in this case is a _very_ bad style of code. – Feuermurmel Feb 01 '14 at 02:05
  • 2
    I disagree. Getting the right answer isn't the only goal, one must also demonstrate understanding of the process. If he thought it necessary to assign to the variable, he didn't understand correctly. And there are likely to be other recursive algorithms where this would be wrong, because they do additional processing after the recursion returns. – Barmar Feb 01 '14 at 02:08
1

I assume that this is in java?

I see two big problems with this snippet.

  1. do is a reserved keyword. It may cause compilation errors, so you should name your method something else.
  2. Executing -= in a parameter call seems quite ambiguous. Will the negation operator run before or after the multiplication by a? The more clear operator to use would be simply the - operator, and it would complete with the same result.

That said, something like this might have been what the teacher was looking for:

public int calculateSomething(int a) {
    if (a == 0) {
        return 1;
    } else {
        return a * calculateSomething(a - 1);
    }
}
elimirks
  • 1,452
  • 2
  • 17
  • 30