Define an action first, then call it when you need it.
The basic function life cycle is: definition → remembered instructions → call → run the body → continue with the program. A definition does not run its body immediately.
def greet():
print("Hello")def tells Python that you are defining a function. greet is the function name, () is function syntax, and : begins its body. The indented print("Hello") belongs to the function, but Python does not run it when it first reads the definition. The empty parentheses mean this function does not receive any values yet; the next concept will show how functions can receive data.
def greet(): defines the action and remembers its instructions. greet() calls that action. Only the call enters the function body and runs print("Hello").
Keep this life cycle in mind: define → Python remembers the instructions → call → run the body → continue. This is why a function can be called more than once without repeating its definition.
print("Before")
def greet():
print("Hello")
print("Defined")
greet()
print("After")Python prints Before, remembers the definition without printing Hello, then prints Defined. At greet(), Python enters the body and prints Hello. When the body ends, execution returns to the next line and prints After.
def show_welcome():
print("Welcome!")
show_welcome()
show_welcome()
show_welcome()Without a function, the same print instruction would be copied three times. A function names one action and lets you call it whenever you need it. If the action changes later, you change its definition in one place.
def ring_bell():
print("Ding!")
ring_bell()
ring_bell()
ring_bell()Each call runs the same indented body again. The definition is written once, while the action can happen as many times as the program needs.
greet()
def greet():
print("Hello")Python executes a program from top to bottom. When it reaches the first greet(), that name has not been defined yet, so the call cannot succeed. Put the definition before the call Python is about to execute.
greet()
show_menu()
print_report()
display_message()A clear function name helps the reader predict what will happen when it is called. Use descriptive snake_case names instead of vague names such as x(), thing(), or do_it().
def greet():
print("Hello")
print("Outside")The indented print belongs to greet(). print("Outside") is not indented, so it is not part of the function and runs in the surrounding program.
A practical function-reading checklist
- Find the definitionWhere does
defdefine the function, and what name does it give the action? - Find the bodyWhich indented instructions belong to that function?
- Find each callWhere does the program use
()to call the function? - Trace the timingWhat runs before the call, during the body, and after the call returns?
- Count the callsEach call runs the body again, so count how many times the action happens.
Common beginner mistakes
- Expecting a definition to run its body
def greet():only defines the function. Withoutgreet(), it does not printHello. - Forgetting parentheses when calling
greet()calls the function. At this level, readgreetas its name andgreet()as the instruction to call it. - Calling before the function is definedPython reaches code from top to bottom, so define the function before the call it reaches.
- Forgetting the colon
def greet()is not a complete definition. The colon starts the function body:def greet():. - Incorrect indentationThe function body must be indented so Python can tell which instructions run when the function is called.
- Confusing a definition with a callVisually separate
def greet():, which defines an action, fromgreet(), which runs it.
