{"id":"return-values-returning","slug":"returning-a-value","title":"Returning a value","text":"The methods so far did their work and gave nothing back: they were `void`. A method can instead **return** a value to the code that called it. The value's type goes where `void` was, and a `return` statement says what to give back:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        int weeks = 3;\n        int days = weeks * daysInWeek();\n        System.out.println(weeks + \" weeks is \" + days + \" days\");\n        System.out.println(daysInWeek());\n    }\n\n    public static int daysInWeek() {\n        return 7;\n    }\n}\n```\n\n```output\n3 weeks is 21 days\n7\n```\n\n- `public static int daysInWeek()` says the method returns an `int`.\n- `return 7;` ends the method and hands 7 back to the call.\n- The call `daysInWeek()` then stands for that value, anywhere a value can go: in a calculation, in a print line, or stored in a variable (`int d = daysInWeek();`).\n\nA call whose value isn't used, like `daysInWeek();` on a line of its own, runs the method, and the value is simply lost.\n\n### return ends the method\n\nNothing after a `return` runs, and Java refuses code that could never run:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        System.out.println(daysInWeek());\n    }\n\n    public static int daysInWeek() {\n        return 7;\n        System.out.println(\"Counted the days\");\n    }\n}\n```\n\n```javac\nMain.java:8: error: unreachable statement\n        System.out.println(\"Counted the days\");\n        ^\n1 error\n```\n\nA `void` method can use a plain `return;` to stop early.\n","task":"Fill in the blanks, so `luckyNumber` returns 7. The check runs `System.out.println(luckyNumber());` and expects:\n\n```\n7\n```\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(\"My lucky number is \" + luckyNumber());\n    }\n\n    public static [[int]] luckyNumber() {\n        [[return]] 7;\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(\"My lucky number is \" + luckyNumber());\n    }\n\n    public static int luckyNumber() {\n        return 7;\n    }\n}\n","hints":["The first blank is the type of the value the method gives back: a whole number.","The second blank is the word that gives the value back: `return`."],"tests":[{"name":"System.out.println(luckyNumber());","stdin":"","call":"System.out.println(luckyNumber());\n","expect":"7","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Write a method `favouriteLanguage()` that returns the String `Java`. The check stores what it returns and prints it in a sentence:\n\n```\nI like Java\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(favouriteLanguage());\n    }\n\n    // Write the favouriteLanguage method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(favouriteLanguage());\n    }\n\n    public static String favouriteLanguage() {\n        return \"Java\";\n    }\n}\n","hints":["The return type is `String`: `public static String favouriteLanguage() {`.","Its only line: `return \"Java\";`"],"tests":[{"name":"Output","stdin":"","call":"String language = favouriteLanguage();\nSystem.out.println(\"I like \" + language);\n","expect":"I like Java","hidden":false}],"require":[],"forbid":[]},{"task":"This method should print `Checked!` and then return whether 10 is more than 3, but it doesn't compile. Fix it, keeping both lines. The check runs `System.out.println(isBigger());` and expects:\n\n```\nChecked!\ntrue\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(isBigger());\n    }\n\n    public static boolean isBigger() {\n        return 10 > 3;\n        System.out.println(\"Checked!\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        System.out.println(isBigger());\n    }\n\n    public static boolean isBigger() {\n        System.out.println(\"Checked!\");\n        return 10 > 3;\n    }\n}\n","hints":["Nothing runs after a `return`, so the print line can never run where it is.","Swap the two lines, so the method prints first and returns last."],"tests":[{"name":"System.out.println(isBigger());","stdin":"","call":"System.out.println(isBigger());\n","expect":"Checked!\ntrue","hidden":false}],"require":[],"forbid":[]}]}