← Python

Functions – Defining

def defines a named, reusable block of code that runs when called

What it does: A function is a reusable, named block of code, created with the def keyword: def greet():. The indented lines underneath — the function body — only run when the function is called by writing its name followed by parentheses: greet(). Defining a function doesn't run it; nothing happens until it's actually called, which is what lets the same code run many times from different places in a program.

Common mistakes: Forgetting the colon at the end of the def line (def greet() instead of def greet():) is the most common error. Another is defining a function but forgetting to call it — the code inside never runs on its own.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a journey and assign it to your class.

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What does def do?
Defines a new functionDeletes a function
Matches
❌ def greet() print('Hi')
Error: Missing colon after def
Applying
Meanings
Term
def
Meaning
Keyword that creates (defines) a new function.
Practice
def greet():
Testing
Gaps
greet():
Check Answer
Spots
1def greet()
2 name = 'World'

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Pufferfish' badge — Error Handler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What does def do?
A. Defines a new function ✓
B. Deletes a function
def creates a new function — it doesn't run it
2. How do you call a function named greet?
A. def greet()
B. greet() ✓
def defines the function — to run it, write the name with parentheses
3. def say_hi():\n print('Hi')\nDoes this print anything?
A. No defining doesn't run it ✓
B. Yes prints Hi
Defining a function stores it — you must call it separately to run it
4. What goes at the end of a def line?
A. A semicolon ;
B. A colon : ✓
def lines end with a colon — the function body is indented below
5. Why use functions?
A. To reuse code without rewriting it ✓
B. To make code slower
Functions let you write code once and call it multiple times

Match the Pairs

def greet() print('Hi') Error: Missing colon after def
Def say_hi(): print('Hi') Error: 'def' is lowercase
def greet(): print('Hello') Valid: Function defined
def greet(): print('Hi') greet() Ok: Define then call
def say_hi(): print('Hi!') say_hi() Output: Hi!
def say_hi(): print('Hi!') Output: (nothing)

Key Term

def
Keyword that creates (defines) a new function.~~Function

Fill in the Blanks

1. def greet(): print('Hello!')
2. def greet(): print('Hello!')
3. def greet(): print('Hello!') greet()
4. def say_hi(): print('Hi!') say_hi()
5. def welcome(): print('Welcome!') welcome()

Spot the Error

Question 1
A def greet()
B name = 'World'
C print('Hello, ' + name)
D print('Goodbye!')
E greet()
Line A — Missing colon after the function definition
Question 2
A def greet():
B name = 'World'
C print('Hello, ' + name)
D print('Goodbye!')
E greet()
Line C — Code inside the function must be indented
Question 3
A Def greet():
B name = 'World'
C print('Hello, ' + name)
D print('Goodbye!')
E greet()
Line A — def must be lowercase
Question 4
A def greet:
B name = 'World'
C print('Hello, ' + name)
D print('Goodbye!')
E greet()
Line A — Missing parentheses: even with no parameters, use def greet():
Question 5
A def greet():
B print('Hello!')
C
D def greet()
Line D — To call a function, just use greet(): don't put def before it
For Teachers

Create a free class, add this module to a journey, and track who's stuck.

Create a free teacher account →

No credit card. No setup fee. Make a class and invite students in under three minutes.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?