{"id":"functions-calling","slug":"calling","title":"Calling functions","text":"A function call makes Python jump into the function's body, run it, and then come back to just after the call. That's why the order of the lines matters.\n\n### Define before you call\n\nPython runs a program from top to bottom, and a `def` is a line like any other: until it has run, the name doesn't exist. So a call above the definition fails:\n\n```python\ngreet()\n\n\ndef greet():\n    print(\"Hello!\")\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 1, in <module>\n    greet()\n    ^^^^^\nNameError: name 'greet' is not defined\n```\n\nPut your definitions at the top of the program and the code that uses them below.\n\n### Functions that call functions\n\nA function's body can call other functions, your own included. Here `title` calls `line` twice:\n\n```python\ndef line():\n    print(\"-\" * 20)\n\n\ndef title():\n    line()\n    print(\"   Weekly report\")\n    line()\n\n\ntitle()\nprint(\"All tasks done.\")\n```\n\n```output\n--------------------\n   Weekly report\n--------------------\nAll tasks done.\n```\n\nFollow the calls. `title()` jumps into the body of `title`. Its first line calls `line()`, which prints the dashes and comes back into `title`, exactly where it left off. Then the heading is printed, `line()` runs once more, and `title` is done, so Python comes back to the main program and prints the last line.\n\nA function can call another function that's defined further down in the file. What matters is that the other function has been defined by the time the call actually runs: the body of `title` runs only when `title()` is called, and by then both definitions have run.\n\n### Don't forget the parentheses\n\nThe parentheses are what makes a call. The name on its own only refers to the function, and nothing happens:\n\n```python\ndef beep():\n    print(\"Beep!\")\n\n\nbeep\nprint(\"Was there a beep?\")\n```\n\n```output\nWas there a beep?\n```\n\nPython doesn't even complain: a line with just a name is allowed, it simply does nothing. If a function of yours seems never to run, check that the call has its `()`.\n","task":"Fill in the blanks so that `banner` uses the function `stars` for its first and last line. When `banner()` is called, it should print:\n\n```\n**********\n* SALE!  *\n**********\n```\n","kind":"fill","seed":"def stars():\n    print(\"**********\")\n\n\ndef banner():\n    [[stars()]]\n    print(\"* SALE!  *\")\n    [[stars()]]\n","solution":"def stars():\n    print(\"**********\")\n\n\ndef banner():\n    stars()\n    print(\"* SALE!  *\")\n    stars()\n","hints":["The first and the last line of the banner are exactly what `stars` prints.","Call `stars` in each blank: its name with parentheses after it."],"tests":[{"name":"banner()","stdin":"","call":"banner()\n","expect":"**********\n* SALE!  *\n**********","hidden":false},{"name":"Test 2","stdin":"","call":"banner()\nstars()\n","expect":"**********\n* SALE!  *\n**********\n**********","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write two functions. `countdown()` prints the numbers 3, 2 and 1, each on its own line, using a loop. `launch()` calls `countdown()` and then prints `Liftoff!`. When `launch()` is called, it prints:\n\n```\n3\n2\n1\nLiftoff!\n```\n","kind":"code","seed":"def countdown():\n    pass\n\n\ndef launch():\n    pass\n","solution":"def countdown():\n    number = 3\n    while number > 0:\n        print(number)\n        number -= 1\n\n\ndef launch():\n    countdown()\n    print(\"Liftoff!\")\n","hints":["In `countdown`, write the loop as you would in a program: a helper variable that starts at 3, a condition, and an update. All of it is indented inside the function.","`launch` has two lines: the call `countdown()` and the `print`."],"tests":[{"name":"launch()","stdin":"","call":"launch()\n","expect":"3\n2\n1\nLiftoff!","hidden":false},{"name":"countdown()","stdin":"","call":"countdown()\n","expect":"3\n2\n1","hidden":true},{"name":"Test 3","stdin":"","call":"launch()\nlaunch()\n","expect":"3\n2\n1\nLiftoff!\n3\n2\n1\nLiftoff!","hidden":true}],"require":[{"pattern":"\\bwhile\\b","message":"Print the countdown with a `while` loop."},{"pattern":"countdown\\s*\\(\\s*\\)","min":2,"message":"`launch` should call `countdown()` instead of printing the numbers itself."}],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"def a():\n    print(\"A starts\")\n    b()\n    print(\"A ends\")\n\n\ndef b():\n    print(\"B\")\n\n\na()\nprint(\"done\")\n","solution":"def a():\n    print(\"A starts\")\n    b()\n    print(\"A ends\")\n\n\ndef b():\n    print(\"B\")\n\n\na()\nprint(\"done\")\n","hints":["`a` calls `b`, which is defined further down. That's fine, because by the time `a()` runs, `b` has been defined.","When `b` is done, Python goes back into `a`, to the line after the call."],"tests":[{"name":"Output","stdin":"","expect":"A starts\nB\nA ends\ndone","hidden":false}],"require":[],"forbid":[],"lines":["A starts","B","A ends","done"]}]}