{"id":"methods-parameters","slug":"parameters","title":"Parameters","text":"A method can take **parameters**: values it gets from the call. They're declared in the parentheses, each with a type and a name, and inside the method they work like variables:\n\n```java\npublic class Main {\n    public static void main(String[] args) {\n        greet(\"Ada\");\n        greet(\"Linus\");\n        String name = \"Grace\";\n        greet(name);\n    }\n\n    public static void greet(String name) {\n        System.out.println(\"Hello, \" + name + \"!\");\n    }\n}\n```\n\n```output\nHello, Ada!\nHello, Linus!\nHello, Grace!\n```\n\n- `String name` in `greet(String name)` is the parameter. Each call gives it a value: `greet(\"Ada\")` runs the method with `name` holding `\"Ada\"`.\n- The value in the call is the **argument**. It can be a literal, a variable or a calculation, such as `greet(first + \" \" + last)`.\n- The argument must fit the parameter's type: `greet(42)` doesn't compile, because 42 isn't a String.\n\nThe same method now does something different on each call, depending on what it's given.\n\n### Challenges that call your method\n\nFrom here on, some challenges ask for a method, and the check calls it directly. The task card shows the call it makes, such as `countdown(3);`, and what that call must print. Your own `main` can call the method however you like: that's what **Run with my input** runs.\n","task":"Fill in the parameter of `shout`, and use it in the print line. The check calls `shout(\"hey\");` and expects:\n\n```\nhey!!!\n```\n","kind":"fill","seed":"public class Main {\n    public static void main(String[] args) {\n        shout(\"hello\");\n        shout(\"goodbye\");\n    }\n\n    public static void shout([[String word]]) {\n        System.out.println([[word]] + \"!!!\");\n    }\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        shout(\"hello\");\n        shout(\"goodbye\");\n    }\n\n    public static void shout(String word) {\n        System.out.println(word + \"!!!\");\n    }\n}\n","hints":["A parameter is a type and a name, like a variable declaration without a value: `String word`.","The print line uses the parameter by its name: `word`."],"tests":[{"name":"shout(\"hey\");","stdin":"","call":"shout(\"hey\");\n","expect":"hey!!!","hidden":false},{"name":"shout(\"Java\");","stdin":"","call":"shout(\"Java\");\n","expect":"Java!!!","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a method `countdown(int start)` that prints the numbers from `start` down to 1, one per line, and then `Liftoff!`. The check calls `countdown(3);` and expects:\n\n```\n3\n2\n1\nLiftoff!\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        countdown(3);\n    }\n\n    // Write the countdown method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        countdown(3);\n    }\n\n    public static void countdown(int start) {\n        for (int i = start; i >= 1; i--) {\n            System.out.println(i);\n        }\n        System.out.println(\"Liftoff!\");\n    }\n}\n","hints":["The method starts `public static void countdown(int start) {`.","A loop counting down: `for (int i = start; i >= 1; i--)`. `Liftoff!` comes after the loop."],"tests":[{"name":"countdown(3);","stdin":"","call":"countdown(3);\n","expect":"3\n2\n1\nLiftoff!","hidden":false},{"name":"countdown(1);","stdin":"","call":"countdown(1);\n","expect":"1\nLiftoff!","hidden":true},{"name":"countdown(0);","stdin":"","call":"countdown(0);\n","expect":"Liftoff!","hidden":true},{"name":"countdown(6);","stdin":"","call":"countdown(6);\n","expect":"6\n5\n4\n3\n2\n1\nLiftoff!","hidden":true}],"require":[],"forbid":[]},{"task":"Write a method `printSquares(int n)` that prints the squares of the numbers from 1 to `n`, one per line: 1 times 1, then 2 times 2, and so on. The check calls `printSquares(4);` and expects:\n\n```\n1\n4\n9\n16\n```\n","kind":"code","seed":"public class Main {\n    public static void main(String[] args) {\n        printSquares(4);\n    }\n\n    // Write the printSquares method here.\n}\n","solution":"public class Main {\n    public static void main(String[] args) {\n        printSquares(4);\n    }\n\n    public static void printSquares(int n) {\n        for (int i = 1; i <= n; i++) {\n            System.out.println(i * i);\n        }\n    }\n}\n","hints":["A loop from 1 to `n`: `for (int i = 1; i <= n; i++)`.","Inside it, print `i * i`."],"tests":[{"name":"printSquares(4);","stdin":"","call":"printSquares(4);\n","expect":"1\n4\n9\n16","hidden":false},{"name":"printSquares(1);","stdin":"","call":"printSquares(1);\n","expect":"1","hidden":true},{"name":"printSquares(7);","stdin":"","call":"printSquares(7);\n","expect":"1\n4\n9\n16\n25\n36\n49","hidden":true}],"require":[],"forbid":[]}]}