×

java · level 1 · unit 7 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

hands the answer back so you can keep it.

So far your methods print things. Printing shows a value on the screen, and then it is gone. Your program cannot catch it, keep it or do maths with it.

To return a value, swap void for a , like int, and end with return and the value. The call then turns into the answer, as if twice(4) were the number 8.

Java checks that the method really hands something back. Forget the return and it stops with missing return statement before anything runs.

public class Main {
    static int twice(int number) {
        return number * 2;
    }
    public static void main(String[] args) {
        int answer = twice(4);
        System.out.println(answer);
        System.out.println(twice(answer));
    }
}

Printing is showing someone your answer, and return is handing it to them.