{"id":"functions-several-parameters","slug":"several-parameters","title":"Several parameters","text":"A function can have several parameters, separated by commas. A call then gives as many arguments, and they're matched by position: the first argument goes to the first parameter, the second to the second, and so on.\n\n```python\ndef introduce(name, age):\n    print(f\"{name} is {age} years old.\")\n\n\nintroduce(\"Mia\", 12)\nintroduce(40, \"Leo\")\n```\n\n```output\nMia is 12 years old.\n40 is Leo years old.\n```\n\nThe second call has its arguments the wrong way round. Python doesn't know what they mean; it simply gives the first one to `name`. Give the arguments in the order of the header.\n\n### Helper variables and decisions\n\nA function's body can have its own helper variables, `if`s and loops, like any program:\n\n```python\ndef compare(a, b):\n    difference = a - b\n    if difference > 0:\n        print(f\"{a} is {difference} more than {b}\")\n    elif difference < 0:\n        print(f\"{a} is {-difference} less than {b}\")\n    else:\n        print(f\"{a} and {b} are equal\")\n\n\ncompare(10, 4)\ncompare(3, 8)\ncompare(5, 5)\n```\n\n```output\n10 is 6 more than 4\n3 is 5 less than 8\n5 and 5 are equal\n```\n\n`difference` is made inside the function, every time it runs. It belongs to the function, and the rest of the program can't see it.\n\n### The names belong to the function\n\nThe names of the parameters have nothing to do with variables outside the function, even when the names are the same. Here the main program has variables called `width` and `height` too:\n\n```python\ndef area(width, height):\n    print(f\"A {width} by {height} rectangle has an area of {width * height}\")\n\n\nwidth = 2\nheight = 3\narea(10, 5)\narea(height, width)\n```\n\n```output\nA 10 by 5 rectangle has an area of 50\nA 3 by 2 rectangle has an area of 6\n```\n\nIn the first call, `width` inside the function is 10, whatever the `width` outside holds. In the second call, the first argument is the outside `height`, 3, so inside the function `width` is 3. Only the order of the arguments counts, never the names in the call.\n","task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"def label(item, price):\n    print(f\"{item} costs {price}\")\n\n\nlabel(\"tea\", 3)\ncost = 5\nlabel(\"cake\", cost + 1)\nlabel(cost, \"pie\")\n","solution":"def label(item, price):\n    print(f\"{item} costs {price}\")\n\n\nlabel(\"tea\", 3)\ncost = 5\nlabel(\"cake\", cost + 1)\nlabel(cost, \"pie\")\n","hints":["The first argument always goes to `item` and the second to `price`.","In the second call, `cost + 1` is worked out first: 6.","In the last call the arguments are the wrong way round, and Python uses them anyway."],"tests":[{"name":"Output","stdin":"","expect":"tea costs 3\ncake costs 6\n5 costs pie","hidden":false}],"require":[],"forbid":[],"lines":["tea costs 3","cake costs 6","5 costs pie"],"more":[{"task":"Write a function `times_table(number, count)` that prints the first `count` lines of the multiplication table of `number`. A call `times_table(3, 4)` prints:\n\n```\n3 x 1 = 3\n3 x 2 = 6\n3 x 3 = 9\n3 x 4 = 12\n```\n","kind":"code","seed":"def times_table(number, count):\n    pass\n","solution":"def times_table(number, count):\n    factor = 1\n    while factor <= count:\n        print(f\"{number} x {factor} = {number * factor}\")\n        factor += 1\n","hints":["Loop with a helper variable that goes from 1 up to `count`.","In each round, print one line with an f-string: the number, ` x `, the helper variable, ` = ` and the product."],"tests":[{"name":"times_table(3, 4)","stdin":"","call":"times_table(3, 4)\n","expect":"3 x 1 = 3\n3 x 2 = 6\n3 x 3 = 9\n3 x 4 = 12","hidden":false},{"name":"times_table(7, 2)","stdin":"","call":"times_table(7, 2)\n","expect":"7 x 1 = 7\n7 x 2 = 14","hidden":true},{"name":"times_table(1, 5)","stdin":"","call":"times_table(1, 5)\n","expect":"1 x 1 = 1\n1 x 2 = 2\n1 x 3 = 3\n1 x 4 = 4\n1 x 5 = 5","hidden":true},{"name":"times_table(12, 1)","stdin":"","call":"times_table(12, 1)\n","expect":"12 x 1 = 12","hidden":true}],"require":[],"forbid":[]},{"task":"Write a function `report(points, maximum)` that prints the points out of the maximum and the percentage, as a whole number rounded down, as below. On the next line it prints `Passed` if the percentage is at least 50, and `Try again` otherwise. A call `report(45, 60)` prints:\n\n```\n45/60 points (75%)\nPassed\n```\n","kind":"code","seed":"def report(points, maximum):\n    pass\n","solution":"def report(points, maximum):\n    percent = points * 100 // maximum\n    print(f\"{points}/{maximum} points ({percent}%)\")\n    if percent >= 50:\n        print(\"Passed\")\n    else:\n        print(\"Try again\")\n","hints":["The percentage is the points times 100, divided by the maximum. `//` divides and rounds down to a whole number.","Keep the percentage in a helper variable, print the first line with an f-string, and then decide with `if` and `else`."],"tests":[{"name":"report(45, 60)","stdin":"","call":"report(45, 60)\n","expect":"45/60 points (75%)\nPassed","hidden":false},{"name":"report(10, 30)","stdin":"","call":"report(10, 30)\n","expect":"10/30 points (33%)\nTry again","hidden":true},{"name":"report(50, 100)","stdin":"","call":"report(50, 100)\n","expect":"50/100 points (50%)\nPassed","hidden":true},{"name":"report(0, 12)","stdin":"","call":"report(0, 12)\n","expect":"0/12 points (0%)\nTry again","hidden":true}],"require":[],"forbid":[]}]}