0

Ok, this must be something really stupid....my statement is returning 2:

  • When I print out the values they are all correct
  • The return statement is wrong somehow

Code:

public static void main (String [] args)
{
    System.out.println(countToTen(1));
}

public static int countToTen(int last_answer){
    last_answer++;
    if(last_answer != 10){
        countToTen(last_answer);
    }
    return  last_answer;
}
4
  • 9
    Hint: you're not doing anything with the return value of countToTen when you call it recursively... Commented May 15, 2014 at 21:02
  • thank you Mark, so why must i do it that way? Please post this answer so i can give you credit Commented May 15, 2014 at 21:03
  • Well, try either debugging through the code or just think about what it's going to return... Commented May 15, 2014 at 21:04
  • 1
    You are not passing the argument by reference, it is a copy. You are doing a modification in last_answer in the context of method, but this change is not propagate outside, because in the end you return last_answer++ Commented May 15, 2014 at 21:11

6 Answers 6

4

Try replacing your if statement with:

if(last_answer != 10){
    return countToTen(last_answer);
}

Without the return statement, the recursive calls do get executed, but the calculated result is never returned.

The order of calls with your broken code looks like:

countToTen(1)
 -> countToTen(2)
 -->  countToTen(3)
 ---> more calls to countToTen()
 --- ... --> countToTen(10) // do nothing, and return to the top-level method call
 -> return 2 // 2 because you incremented it using lastAnswer++
Sign up to request clarification or add additional context in comments.

2 Comments

So why is "return" neccessary, and could you please post it inside your answer for others! Thanks :)
Very good, please up this question for future viewers. I marked it as the answer, thanks again!
2

Your function returns the value from the first call. It is the initial value (1) incremented once by the ++ statement, so the function returns 2.

Integers are passed by value in Java, incrementing the passed value inside the function does not change the value outside:

int x = 0;
v(x);
// x still 0 here.

void v(int x) {
  x = 100;
}

1 Comment

Thank you, this is the explanation I was looking for!
0

Try this:

public static int countToTen(int last_answer){

  last_answer++;

  if(last_answer != 10){
    return countToTen(last_answer);
  }
  else {
    return last_answer;
  }
} 

2 Comments

Thats the same thing except now it wont compile now because there isn't a mandatory return.
It will compile. All code paths return a value (there is no code path which bypasses both of Tyler's return statements). Some people prefer if(condition){return value;} else {return othervalue;} over if(condition){return value;} return othervalue;, as it makes it more obvious that return othervalue; only happens if condition is false.
0

If you want to print out 1,2,3,4 ... 10, you need to print out the answer in every phase separately

public static void main (String [] args){
    countToTen(1);
}

public static void countToTen(int last_answer){

    System.out.println(last_answer);
    last_answer++;

    if(last_answer <= 10){
    countToTen(last_answer);
    }
} 

1 Comment

Hi Sarah, no i was just wondering why my ending return was 2 and not 10
0

My proposal

public static int countToTen(int last_answer){

   last_answer++;

    if(last_answer < 10){
        return countToTen(last_answer);
    } else {
        return last_answer;
    }
}

You are not passing the argument by reference, it is a copy. You are doing a modification in last_answer in the context of method, but this change is not propagate outside, because in the end you return last_answer++

Comments

0
public static void main (String [] args)
{
    System.out.println(countToTen(1));    
}

public static int countToTen(int last_answer)
{    
    last_answer++;

    if(last_answer != 10)
    {
        return countToTen(last_answer); //Here was the error
    }

    return  last_answer;
} 

Comments

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.