Code

Your First Spell

A function is a named recipe of code. You write it once, then run it whenever you like by calling it.

def light_lantern():
    print("Lantern lit!")
    print("The dark retreats.")

light_lantern()
light_lantern()
  • def starts a function definition, followed by a name, parentheses () and a colon :.
  • The indented lines are the function's body.
  • Defining a function does not run it. The body only runs when you call it with light_lantern(). Don't forget the ()!

Pip tip: Name functions after what they do: light_lantern, count_coins, greet. Use lowercase words joined by _.

Assignment

  1. Define a function named cheer that prints Hip hip! and then Hooray! (two lines).
  2. Below the definition, call cheer() twice.