{"id":"return-values-return","slug":"return","title":"Returning a value","text":"Some functions give something back. `input` gives back the text the user typed, `len` gives back a length and `int` gives back a number. Python replaces the call with that value, which you can store in a variable or use in a calculation:\n\n```python\ncount = len(\"hello\") + 1\n```\n\nThe value a function gives back is its **return value**. Your own functions give one back with the **`return`** statement:\n\n```python\ndef double(number):\n    return number * 2\n\n\nresult = double(21)\nprint(result)\nprint(double(5) + 1)\n```\n\n```output\n42\n11\n```\n\n`double` prints nothing itself. It works out `number * 2` and returns it, and the code that called it decides what to do with it: the first call stores it in `result`, the second adds 1 to it.\n\n### return ends the function\n\nA `return` ends the function right away, even in the middle of a loop or an `if`. That makes it easy to return different values in different cases:\n\n```python\ndef sign(number):\n    if number > 0:\n        return \"positive\"\n    if number < 0:\n        return \"negative\"\n    return \"zero\"\n\n\nprint(sign(15))\nprint(sign(-3))\nprint(sign(0))\n```\n\n```output\npositive\nnegative\nzero\n```\n\nFor 15 the first `return` runs and the function is over: the lines below it never run for that call. Only when neither condition is true does Python reach the last line. One call runs exactly one `return`.\n\nA function can return anything: a number, a string, `True` or `False`. Here the function reads a number, so the code that uses it stays short:\n\n```python\ndef ask_number(prompt):\n    return int(input(prompt))\n\n\nwidth = ask_number(\"Width: \")\nheight = ask_number(\"Height: \")\nprint(\"Area:\", width * height)\n```\n\n```input\n4\n5\n```\n\n```output\nWidth: 4\nHeight: 5\nArea: 20\n```\n\n### A return without a value\n\nA plain `return`, with no value after it, just ends the function. It's handy in a function that prints, to stop early when there's nothing sensible to do:\n\n```python\ndef print_ratio(a, b):\n    if b == 0:\n        print(\"Can't divide by zero\")\n        return\n    print(a / b)\n\n\nprint_ratio(7, 2)\nprint_ratio(7, 0)\n```\n\n```output\n3.5\nCan't divide by zero\n```\n\nIn a function task, the checks call your function and show what it returns the way the interpreter does: `42` for a number, `'positive'` with quotes for a string.\n","task":"Fill in the blank so that `area(width, height)` returns the area of a rectangle. For example, `area(3, 4)` returns `12`.\n","kind":"fill","seed":"def area(width, height):\n    [[return]] width * height\n\n\nif __name__ == \"__main__\":\n    print(area(3, 4))\n","solution":"def area(width, height):\n    return width * height\n\n\nif __name__ == \"__main__\":\n    print(area(3, 4))\n","hints":["The function should give the result back to the code that called it, not print it.","The statement that gives a value back is `return`."],"tests":[{"name":"area(3, 4)","stdin":"","call":"area(3, 4)\n","expect":"12","hidden":false,"returns":true},{"name":"area(7, 2)","stdin":"","call":"area(7, 2)\n","expect":"14","hidden":true,"returns":true},{"name":"area(10, 10)","stdin":"","call":"area(10, 10)\n","expect":"100","hidden":true,"returns":true}],"require":[],"forbid":[],"more":[{"task":"A museum's ticket is free for children under 7, costs 8 euros from 7 to 17 years, 12 euros from 18 to 64 years and 6 euros from 65 years on. Write the function `ticket_price(age)`, which returns the price as a whole number. For example, `ticket_price(5)` returns `0` and `ticket_price(30)` returns `12`.\n","kind":"code","seed":"def ticket_price(age):\n    pass\n\n\nif __name__ == \"__main__\":\n    print(ticket_price(5))\n    print(ticket_price(30))\n","solution":"def ticket_price(age):\n    if age < 7:\n        return 0\n    if age < 18:\n        return 8\n    if age < 65:\n        return 12\n    return 6\n\n\nif __name__ == \"__main__\":\n    print(ticket_price(5))\n    print(ticket_price(30))\n","hints":["Check the ages from the youngest up: `if age < 7:` returns `0`.","Since `return` ends the function, the next `if` only runs for ages of 7 or more. So `if age < 18:` returns `8`.","After the last `if`, a final `return 6` covers everyone from 65 on."],"tests":[{"name":"ticket_price(5)","stdin":"","call":"ticket_price(5)\n","expect":"0","hidden":false,"returns":true},{"name":"ticket_price(30)","stdin":"","call":"ticket_price(30)\n","expect":"12","hidden":false,"returns":true},{"name":"ticket_price(7)","stdin":"","call":"ticket_price(7)\n","expect":"8","hidden":true,"returns":true},{"name":"ticket_price(17)","stdin":"","call":"ticket_price(17)\n","expect":"8","hidden":true,"returns":true},{"name":"ticket_price(64)","stdin":"","call":"ticket_price(64)\n","expect":"12","hidden":true,"returns":true},{"name":"ticket_price(65)","stdin":"","call":"ticket_price(65)\n","expect":"6","hidden":true,"returns":true}],"require":[],"forbid":[]},{"task":"Write the function `initials(first_name, last_name)`, which returns a person's initials in capital letters, each followed by a dot. For example, `initials(\"ada\", \"Lovelace\")` returns `'A.L.'`.\n","kind":"code","seed":"def initials(first_name, last_name):\n    pass\n\n\nif __name__ == \"__main__\":\n    print(initials(\"ada\", \"Lovelace\"))\n","solution":"def initials(first_name, last_name):\n    return first_name[0].upper() + \".\" + last_name[0].upper() + \".\"\n\n\nif __name__ == \"__main__\":\n    print(initials(\"ada\", \"Lovelace\"))\n","hints":["The first letter of a string is at index 0: `first_name[0]`.","`.upper()` gives a letter in capitals, and `+` joins strings together.","Return the whole string: `return first_name[0].upper() + \".\" + ...`."],"tests":[{"name":"initials(\"ada\", \"Lovelace\")","stdin":"","call":"initials(\"ada\", \"Lovelace\")\n","expect":"'A.L.'","hidden":false,"returns":true},{"name":"initials(\"Grace\", \"hopper\")","stdin":"","call":"initials(\"Grace\", \"hopper\")\n","expect":"'G.H.'","hidden":true,"returns":true},{"name":"initials(\"guido\", \"van rossum\")","stdin":"","call":"initials(\"guido\", \"van rossum\")\n","expect":"'G.V.'","hidden":true,"returns":true}],"require":[],"forbid":[]}]}