✦ Python Foundations Functions module

Concept 15 · Defining Functions

Name reusable code.
Call it when you need it.

A function gives a name to a reusable action. Define its instructions once, then call the function whenever that action is needed.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

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.

Trace one function call
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.

Why give an action a function name?
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.

One definition, several calls
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.

Define before the call Python reaches
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.

Choose names that describe an action
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().

Indentation marks the function body
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

  1. Find the definitionWhere does def define the function, and what name does it give the action?
  2. Find the bodyWhich indented instructions belong to that function?
  3. Find each callWhere does the program use () to call the function?
  4. Trace the timingWhat runs before the call, during the body, and after the call returns?
  5. 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 bodydef greet(): only defines the function. Without greet(), it does not print Hello.
  • Forgetting parentheses when callinggreet() calls the function. At this level, read greet as its name and greet() 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 colondef 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, from greet(), which runs it.

Python Playground

Loading Python runtime…

Change the message inside greet(), call the function more than once, and run the code. Notice how one definition can produce several executions.

defining_functions.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Read the function definition and each call before choosing an answer.

def say_hi():
    print("Hi!")

print("Before")
say_hi()
print("After")
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.