{"id":"terminology-functions","slug":"functions","title":"Functions, arguments and return values","text":"A **function** is a named piece of functionality that a program can use. You have used several already: `print`, `input`, `int`, `float`, `str` and `type`. Using a function is called **calling** it: you write its name followed by parentheses.\n\nThe values inside the parentheses are the function's **arguments**, the data it works on. In `print(\"Score:\", score)` the two arguments are `\"Score:\"` and the value of `score`. Some functions can be called with no arguments at all (`print()` prints an empty line), and some take several, separated by commas. In everyday talk, programmers often say **parameter** when they mean an argument. Strictly, a parameter is the name inside a function that receives an argument, a difference you'll see once you write functions of your own later in the course.\n\n### Return values\n\nMany functions also give back a value, called their **return value**. When the call runs, Python puts the return value in its place, so a function call is an expression. `input` returns what the user typed, as a string, which is why you usually store it in a variable.\n\nA new function shows this well. `len` returns the length of a string: the number of characters in it, with spaces and punctuation included.\n\n```python\nsentence = input(\"Type a sentence: \")\nprint(f\"That's {len(sentence)} characters.\")\n```\n\n```input\nI like tea.\n```\n\n```output\nType a sentence: I like tea.\nThat's 11 characters.\n```\n\nThe letters make up 8 of the 11 characters; the two spaces and the period are the other 3. If the user presses Enter without typing anything, `input` returns the **empty string** `\"\"`, a string with no characters at all, and its length is 0. Since `len(...)` gives an `int`, you can store it, calculate with it or compare it, like any other number:\n\n```python\nname = input(\"Choose a username: \")\nlength = len(name)\nif length < 5:\n    print(f\"{name} has only {length} characters. Use at least 5.\")\n```\n\n```input\nada\n```\n\n```output\nChoose a username: ada\nada has only 3 characters. Use at least 5.\n```\n\n### Calls inside calls\n\nSince a call is an expression, it can be the argument of another call. You have done this since part 1: in `int(input(\"Age: \"))`, Python first calls `input`, and its return value, a string, becomes the argument of `int`. The conversion functions `int`, `float` and `str` are functions like any other: each takes one argument and returns its value as another type.\n\nNot every argument works with every function. `len` counts the characters of a string, but a number has no characters to count:\n\n```python\nyear = 2026\nprint(len(year))\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    print(len(year))\n          ~~~^^^^^^\nTypeError: object of type 'int' has no len()\n```\n\nThe message says that a value of the type `int` has no length. Convert the number into a string first, and then count: `len(str(year))` is 4, since `str(year)` is the string `\"2026\"`.\n","task":"Fill in the blank with a function call, so the program prints how many characters the word has:\n\n```\nType a word: keyboard\nThe word keyboard has 8 characters.\n```\n","kind":"fill","seed":"word = input(\"Type a word: \")\nlength = [[len(word)]]\nprint(f\"The word {word} has {length} characters.\")\n","solution":"word = input(\"Type a word: \")\nlength = len(word)\nprint(f\"The word {word} has {length} characters.\")\n","hints":["The function that gives the number of characters in a string is `len`.","Call it with the variable as its argument: `len(word)`."],"tests":[{"name":"Test 1","stdin":"keyboard\n","expect":"Type a word: keyboard\nThe word keyboard has 8 characters.","hidden":false},{"name":"Test 2","stdin":"Python Arena\n","expect":"Type a word: Python Arena\nThe word Python Arena has 12 characters.","hidden":true},{"name":"Test 3","stdin":"a\n","expect":"Type a word: a\nThe word a has 1 characters.","hidden":true}],"require":[],"forbid":[],"more":[{"task":"The starter code reads a whole number with `int`. Add a line that prints how many digits the number has. Calling `len` with the number itself stops with an error, so convert it first.\n\n```\nType a whole number: 2026\nThe number 2026 has 4 digits.\n```\n","kind":"code","seed":"number = int(input(\"Type a whole number: \"))\n","solution":"number = int(input(\"Type a whole number: \"))\nprint(f\"The number {number} has {len(str(number))} digits.\")\n","hints":["`len` counts the characters of a string, so turn the number into a string with `str(number)` first.","Put one call inside the other: `len(str(number))`. Python calls `str` first and gives its return value to `len`.","The whole expression can go inside the braces of an f-string."],"tests":[{"name":"Test 1","stdin":"2026\n","expect":"Type a whole number: 2026\nThe number 2026 has 4 digits.","hidden":false},{"name":"Test 2","stdin":"7\n","expect":"Type a whole number: 7\nThe number 7 has 1 digits.","hidden":true},{"name":"Test 3","stdin":"1000000\n","expect":"Type a whole number: 1000000\nThe number 1000000 has 7 digits.","hidden":true}],"require":[{"pattern":"\\blen\\s*\\(\\s*str\\s*\\(","raw":true,"message":"Count the digits with `len(str(...))`."}],"forbid":[]},{"task":"A status update can be at most 30 characters long. Write a program that asks for a status, prints how many characters it has, and if it has more than 30, also prints how many characters too long it is.\n\n```\nStatus: Learning Python one step at a time\n34 characters\nToo long by 4 characters.\n```\n","kind":"code","seed":"status = input(\"Status: \")\n","solution":"status = input(\"Status: \")\nlength = len(status)\nprint(f\"{length} characters\")\nif length > 30:\n    print(f\"Too long by {length - 30} characters.\")\n","hints":["`len(status)` gives the number of characters. Store it in a variable, since you need it twice.","Print the first line with an f-string, then use `if length > 30:` for the second one.","The number of extra characters is the length minus 30."],"tests":[{"name":"Test 1","stdin":"Learning Python one step at a time\n","expect":"Status: Learning Python one step at a time\n34 characters\nToo long by 4 characters.","hidden":false},{"name":"Test 2","stdin":"Hello!\n","expect":"Status: Hello!\n6 characters","hidden":true},{"name":"Test 3","stdin":"This status is exactly 30 long\n","expect":"Status: This status is exactly 30 long\n30 characters","hidden":true}],"require":[],"forbid":[]}]}