← Years 7–8 Creating Digital Solutions

Defining a function with def

Creates a function: a named, reusable block of indented code that only runs when you call it. Define it first, then call it.

Understanding Defining a function with def in Years 7–8 Creating Digital Solutions

What it does: The def block creates the function, and the call cheer() on the last line runs it. The definition comes before the call. The name say_hello is followed by () and a colon. The print line is indented so it is part of the function body. Python uses the keyword def to define a function. It does not use function. Function names cannot have spaces. Use an underscore like show_score instead.

Common mistakes: Text inside print() must be in quote marks. It should be ' print("Hi!")'. A def line must end with a colon. It should be 'def welcome():'.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 50–125) and word search (Modules 3–125).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which line correctly starts a function called greet?
function greet():def greet():
Matches
def greet() print("Hi!")
↓
The colon is missing at the end of the def line. It should be def greet():
Applying
Meanings
Term
def name():
Meaning
Creates a function: a named, reusable block of indented code that only runs when you call it. Define it first, then call it.
Practice
def cheer():
Testing
Gaps
greet():
Check Answer
Spots
1def greet():
2 print(Hi!)

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

The 'Penguin' badge — List Waddler, 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. Which line correctly starts a function called greet?
A. function greet():
B. def greet(): ✓
Python uses the keyword def to define a function. It does not use function.
2. When does the code inside a function run?
A. Only when the function is called ✓
B. As soon as Python reads the def line
Defining a function just saves the code. It only runs when you call it, like greet().
3. Which function definition is written correctly?
A. def hello(): print("Hi!") ✓
B. def hello(): print("Hi!")
The body of a function must be indented. Without the indent, Python gives an error.
4. Which def line is correct?
A. def say_hi()
B. def say_hi(): ✓
The def line must end with a colon : so Python knows the indented body comes next.
5. Which order will work without an error?
A. Define the function first, then call it ✓
B. Call the function first, then define it
Python reads from top to bottom, so the function must be defined before the line that calls it.

Match the Pairs

def greet(): → Defines a function named greet. Its code does not run yet.
print("Hi!") → An indented line inside the function body that runs only when the function is called.
Define the function first, then call it to make the body run. → def cheer(): print("Go!") cheer()
A function name is followed by brackets and a colon, and the body is indented. → def say_hello(): print("Hello")
def greet() print("Hi!") → The colon is missing at the end of the def line. It should be def greet():
greet() def greet(): print("Hi!") → The function is called before it is defined, so Python gives a NameError. Define it first, then call it.

Key Term

def name():
Creates a function: a named, reusable block of indented code that only runs when you call it. Define it first, then call it.

Fill in the Blanks

1. def greet(): print("Hi!") greet()
Hint: Type the keyword that starts creating a function
2. def greet(): print("Hello!") greet()
Hint: Type the function name that matches the call on the last line
3. def welcome(): print("Welcome!") welcome()
Hint: Type the punctuation mark that ends the def line before the indented body
4. def say_hi(): print("Hi!") say_hi()
Hint: Type the name the function must be given so the call on the last line works
5. def beep(): print("Beep!") beep()
Hint: Type the built-in function used inside the indented body to show text

Spot the Error

Question 1
A def greet():
B print(Hi!)
C greet()
Line B — Text inside print() must be in quote marks. It should be ' print("Hi!")'.
Question 2
A def welcome()
B print("Welcome")
C welcome()
Line A — A def line must end with a colon. It should be 'def welcome():'.
Question 3
A def cheer():
B print("Go team!"
C cheer()
D cheer()
Line B — The closing bracket is missing, so the function body has a syntax error. It should be ' print("Go team!")'.
Question 4
A def say_hi():
B Print("Hi")
C say_hi()
Line B — Python is case-sensitive. The built-in function is print with a lowercase p, so it should be ' print("Hi")'.
Question 5
A def party():
B print("Cake")
C party
Line C — Writing the name without brackets does not call the function, so nothing runs. It should be 'party()'.

Student with a class code? Join your class →