{"id":"functions-parameters","slug":"parameters","title":"Parameters and arguments","text":"`print` prints whatever you give it in the parentheses. Your own functions can take values too. Name a variable in the parentheses of the header, and the function gets a value in it every time it's called:\n\n```python\ndef greet(name):\n    print(f\"Hello, {name}!\")\n\n\ngreet(\"Ada\")\ngreet(\"Linus\")\n```\n\n```output\nHello, Ada!\nHello, Linus!\n```\n\nThe variable in the header, `name`, is a **parameter**. The value in the call, `\"Ada\"`, is an **argument**. When the call runs, the parameter gets the argument as its value, and then the body runs. Each call can give a different argument, so one function does the same work with different values.\n\nThe two words are easy to mix up, and people often use them loosely, but the difference is worth knowing: a parameter is the name the function uses inside, and an argument is the value a call gives it from outside.\n\n### Any value as an argument\n\nAn argument can be any expression: a value written out, a variable, or a calculation. Python works out its value first and passes on the result:\n\n```python\ndef show_square(number):\n    print(f\"{number} squared is {number * number}\")\n\n\nshow_square(4)\nside = 7\nshow_square(side)\nshow_square(side + 1)\n```\n\n```output\n4 squared is 16\n7 squared is 49\n8 squared is 64\n```\n\nIn the call `show_square(side)`, the parameter `number` gets the value of `side`, 7. The names don't have to match: the function only ever sees the value.\n\nInside the body, a parameter is a variable like any other. You can use it in a calculation, an `if`, a loop or an f-string, and even change it: the change stays inside the function.\n\n### A missing argument\n\nA function with a parameter needs an argument in every call:\n\n```python\ndef greet(name):\n    print(f\"Hello, {name}!\")\n\n\ngreet()\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 5, in <module>\n    greet()\n    ~~~~~^^\nTypeError: greet() missing 1 required positional argument: 'name'\n```\n\nThe message names the function and the parameter that got no value. \"Positional\" means that arguments are matched to parameters by their position, first to first. That matters as soon as a function has more than one parameter, in the next step.\n","task":"Fill in the parameter so the function greets the guest it's called with. A call `welcome(\"Grace\")` should print:\n\n```\nWelcome, Grace!\nYour table is ready, Grace.\n```\n","kind":"fill","seed":"def welcome([[guest]]):\n    print(f\"Welcome, {guest}!\")\n    print(f\"Your table is ready, {guest}.\")\n","solution":"def welcome(guest):\n    print(f\"Welcome, {guest}!\")\n    print(f\"Your table is ready, {guest}.\")\n","hints":["The body uses a variable that has no value yet. It should get its value from the call.","The parameter goes in the parentheses of the header. Its name is the one the body uses: `guest`."],"tests":[{"name":"welcome(\"Grace\")","stdin":"","call":"welcome(\"Grace\")\n","expect":"Welcome, Grace!\nYour table is ready, Grace.","hidden":false},{"name":"welcome(\"Tim\")","stdin":"","call":"welcome(\"Tim\")\n","expect":"Welcome, Tim!\nYour table is ready, Tim.","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a function `underline(text)` that prints the text and, on the line under it, as many `=` characters as the text has. A call `underline(\"Hello\")` prints:\n\n```\nHello\n=====\n```\n","kind":"code","seed":"def underline(text):\n    pass\n","solution":"def underline(text):\n    print(text)\n    print(\"=\" * len(text))\n","hints":["The parameter `text` holds the string the call gives. Print it first.","The line under it is `\"=\"` repeated `len(text)` times."],"tests":[{"name":"underline(\"Hello\")","stdin":"","call":"underline(\"Hello\")\n","expect":"Hello\n=====","hidden":false},{"name":"underline(\"Python Arena\")","stdin":"","call":"underline(\"Python Arena\")\n","expect":"Python Arena\n============","hidden":true},{"name":"underline(\"x\")","stdin":"","call":"underline(\"x\")\n","expect":"x\n=","hidden":true}],"require":[],"forbid":[]},{"task":"Write a function `countdown(start)` that prints the numbers from `start` down to 1 on one line, each followed by a space, and then `Go!`. A call `countdown(3)` prints:\n\n```\n3 2 1 Go!\n```\n\nWith 0 as the argument, it prints only `Go!`.\n","kind":"code","seed":"def countdown(start):\n    pass\n","solution":"def countdown(start):\n    line = \"\"\n    while start > 0:\n        line += f\"{start} \"\n        start -= 1\n    line += \"Go!\"\n    print(line)\n","hints":["Build the line in a string that starts empty. A loop adds each number and a space, counting down from `start`.","The parameter `start` can be the loop's helper variable: `while start > 0:`, with `start -= 1` in the body.","After the loop, add `Go!` to the string and print it."],"tests":[{"name":"countdown(3)","stdin":"","call":"countdown(3)\n","expect":"3 2 1 Go!","hidden":false},{"name":"countdown(0)","stdin":"","call":"countdown(0)\n","expect":"Go!","hidden":true},{"name":"countdown(1)","stdin":"","call":"countdown(1)\n","expect":"1 Go!","hidden":true},{"name":"countdown(7)","stdin":"","call":"countdown(7)\n","expect":"7 6 5 4 3 2 1 Go!","hidden":true}],"require":[],"forbid":[]}]}