{"id":"functions-define","slug":"define","title":"Your own functions","text":"You have called plenty of functions already: `print`, `input`, `int`, `len`. They come with Python. Now you'll write your own. A **function** is a named piece of code: you write it once, and then run it whenever you need it, by its name.\n\n### Defining a function\n\n```python\ndef greet():\n    print(\"Hello!\")\n    print(\"Nice to see you.\")\n```\n\nA function **definition** starts with the keyword `def` (short for define), then the function's name, a pair of parentheses and a colon. This first line is the function's **header**. Under it, indented like the body of an `if` or a `while`, comes the function's **body**: the code that runs when the function is used.\n\nRun that program and nothing happens. That's not a mistake: `def` only defines the function, that is, it tells Python what the name `greet` means. The body runs only when the function is **called**.\n\n### Calling a function\n\nTo call a function, write its name with parentheses after it, as you do with `print`:\n\n```python\ndef greet():\n    print(\"Hello!\")\n    print(\"Nice to see you.\")\n\n\ngreet()\nprint(\"...\")\ngreet()\n```\n\n```output\nHello!\nNice to see you.\n...\nHello!\nNice to see you.\n```\n\nPython reads the definition and remembers the function. Then each call runs its body, once per call. A function can be called as many times as you like: that's the point of writing one.\n\nFunction names follow the same rules as variable names: small letters, with underscores between words, such as `print_menu`. Leave two empty lines after a function definition, before the code that comes after it; Python doesn't need them, but they make the program easier to read, and Python programmers expect them.\n\n### Functions on this site\n\nFrom now on, many tasks ask you to write a function. The checks call your function themselves, so your program needs only the definition, with no calls of its own and no `input`. A test named `greet()` calls `greet()` and compares what it prints with what it should print. To try your function with **Run**, you can add a call under the definition; the checks look only at what their own calls print.\n\nThe starter code of a function task often has `pass` as the body. `pass` is a statement that does nothing. A body can't be empty, so `pass` holds the place until you write the real code. Replace it with your own lines.\n","task":"Fill in the blank so the program defines a function `cheer` that prints `Hip hip hooray!` when it's called.\n","kind":"fill","seed":"[[def]] cheer():\n    print(\"Hip hip hooray!\")\n","solution":"def cheer():\n    print(\"Hip hip hooray!\")\n","hints":["A function definition starts with a keyword, followed by the function's name, parentheses and a colon.","The keyword is short for \"define\"."],"tests":[{"name":"cheer()","stdin":"","call":"cheer()\n","expect":"Hip hip hooray!","hidden":false},{"name":"Test 2","stdin":"","call":"cheer()\ncheer()\n","expect":"Hip hip hooray!\nHip hip hooray!","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a function `print_menu()` that prints this menu:\n\n```\n1. Start\n2. Settings\n3. Quit\n```\n","kind":"code","seed":"def print_menu():\n    pass\n","solution":"def print_menu():\n    print(\"1. Start\")\n    print(\"2. Settings\")\n    print(\"3. Quit\")\n","hints":["Replace `pass` with three `print` lines, indented by four spaces like `pass` is.","The checks call `print_menu()` themselves, so the program needs no call of its own."],"tests":[{"name":"print_menu()","stdin":"","call":"print_menu()\n","expect":"1. Start\n2. Settings\n3. Quit","hidden":false},{"name":"Test 2","stdin":"","call":"print_menu()\nprint_menu()\n","expect":"1. Start\n2. Settings\n3. Quit\n1. Start\n2. Settings\n3. Quit","hidden":true}],"require":[],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"def chorus():\n    print(\"la la la\")\n\n\nprint(\"verse\")\nchorus()\nprint(\"verse\")\nchorus()\n","solution":"def chorus():\n    print(\"la la la\")\n\n\nprint(\"verse\")\nchorus()\nprint(\"verse\")\nchorus()\n","hints":["The definition prints nothing by itself. The body of `chorus` runs only when it's called.","Go through the lines below the definition from top to bottom: each `chorus()` prints the line in its body."],"tests":[{"name":"Output","stdin":"","expect":"verse\nla la la\nverse\nla la la","hidden":false}],"require":[],"forbid":[],"lines":["verse","la la la","verse","la la la"]}]}