{"id":"return-values-local-variables","slug":"variables-inside-methods","title":"Variables inside methods","text":"A variable made inside a method belongs to that method: it's a **local variable**. It exists while the method runs and disappears when the method ends. Other methods can't see it, not even `main`. The same goes for parameters.\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        addTax(100);\n        System.out.println(withTax);\n    }\n\n    public static double addTax(double price) {\n        double withTax = price * 1.25;\n        return withTax;\n    }\n}\n```\n\n```javac\nMain.java:4: error: cannot find symbol\n        System.out.println(withTax);\n                           ^\n  symbol:   variable withTax\n  location: class Main\n1 error\n```\n\n`withTax` lives only inside `addTax`. To get its value out, use the return value: store what the call returns in a variable of `main`'s own.\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        double withTax = addTax(100);\n        System.out.println(withTax);\n    }\n\n    public static double addTax(double price) {\n        double withTax = price * 1.25;\n        return withTax;\n    }\n}\n```\n\n```output\n125.0\n```\n\nThe two `withTax` variables are different variables that happen to share a name, one in each method.\n\nAnother mistake gives the same `cannot find symbol` error: using a method's name without the parentheses and arguments, as `square` instead of `square(5)`. Without them, Java looks for a variable called `square`.\n","task":"`main` tries to use the method's variable `total`, so the program doesn't compile. Fix `main` by using the value `calculateTotal` returns. The program prints:\n\n```\nTotal: 7\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        calculateTotal(3, 4);\n        System.out.println(\"Total: \" + total);\n    }\n\n    public static int calculateTotal(int first, int second) {\n        int total = first + second;\n        return total;\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int total = calculateTotal(3, 4);\n        System.out.println(\"Total: \" + total);\n    }\n\n    public static int calculateTotal(int first, int second) {\n        int total = first + second;\n        return total;\n    }\n}\n","hints":["The `total` inside `calculateTotal` doesn't exist in `main`.","Store the returned value: `int total = calculateTotal(3, 4);`"],"tests":[{"name":"The program","stdin":"","expect":"Total: 7","hidden":false},{"name":"System.out.println(calculateTotal(10, 5));","stdin":"","call":"System.out.println(calculateTotal(10, 5));\n","expect":"15","hidden":true}],"require":[{"pattern":"\\bcalculateTotal\\s*\\(\\s*3\\s*,\\s*4\\s*\\)","message":"Keep the call `calculateTotal(3, 4)`, and print the value it returns."}],"forbid":[{"pattern":"\\b7\\b","message":"Print the value `calculateTotal` returns, not a total you worked out yourself."}],"more":[{"task":"`main` should print the square of 9, but it doesn't compile. Fix the call. The program prints:\n\n```\n81\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        int result = square;\n        System.out.println(result);\n    }\n\n    public static int square(int number) {\n        return number * number;\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int result = square(9);\n        System.out.println(result);\n    }\n\n    public static int square(int number) {\n        return number * number;\n    }\n}\n","hints":["A method is called with parentheses, and `square` needs a number in them.","`int result = square(9);`"],"tests":[{"name":"The program","stdin":"","expect":"81","hidden":false}],"require":[{"pattern":"\\bsquare\\s*\\(\\s*9\\s*\\)","message":"Call `square(9)` and print what it returns."}],"forbid":[{"pattern":"\\b81\\b","message":"Let `square` work out the answer instead of typing it in."}]},{"task":"A ticket costs whatever `ticketPrice()` returns. Call it only once, store the price in a variable, and print the price of one, two and three tickets. The program prints:\n\n```\nOne ticket: 12\nTwo tickets: 24\nThree tickets: 36\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n\n    }\n\n    public static int ticketPrice() {\n        return 12;\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        int price = ticketPrice();\n        System.out.println(\"One ticket: \" + price);\n        System.out.println(\"Two tickets: \" + 2 * price);\n        System.out.println(\"Three tickets: \" + 3 * price);\n    }\n\n    public static int ticketPrice() {\n        return 12;\n    }\n}\n","hints":["`int price = ticketPrice();` calls the method once and keeps the value.","Then use `price` in all three lines, for example `2 * price`."],"tests":[{"name":"The program","stdin":"","expect":"One ticket: 12\nTwo tickets: 24\nThree tickets: 36","hidden":false}],"require":[{"pattern":"\\bticketPrice\\s*\\(\\s*\\)","min":2,"max":2,"message":"Call `ticketPrice()` once, and use the variable for all three lines."},{"pattern":"=\\s*ticketPrice\\s*\\(\\s*\\)\\s*;","message":"Store what `ticketPrice()` returns in a variable: `int price = ticketPrice();`"}],"forbid":[{"pattern":"\\b(24|36)\\b","message":"Calculate the prices of two and three tickets from the variable, instead of typing them in."}]}]}