60-second card
Defining functions
A function is a named block of code you can run again whenever you need it.
- Start it with def, a name, brackets and a colon.
- The indented lines are the body; they run only when you call the function.
- Call it with its name and brackets: greet().
def greet():
print("Namaste!")
greet()
greet()Prints
Namaste!
Namaste!Remember: Defining a function doesn't run it. Calling it does.
Common mistake: Calling a function before its def line. Python reads top to bottom, so it isn't defined yet.