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);
    }
}
1
  • Please tag your questions with the language you're using, it will help the correct experts find your questions. Commented Feb 1, 2014 at 1:49

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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 stackoverflow.com/questions/6800590/…
Nonetheless, it APPEARS to be quite ambiguous.
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.
I'm not a Java programmer, and didn't know it was an exception to that rule.
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.
|
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);
    }
}

1 Comment

Updated to simply say that using the negation operator would be more clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.