Defining a function with def

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Defining a function with def

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line correctly starts a function called greet?
A function greet():
B def greet():
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
3. Which function definition is written correctly?
A def hello(): print("Hi!")
B def hello(): print("Hi!")
4. Which def line is correct?
A def say_hi()
B def say_hi():
5. Which order will work without an error?
A Define the function first, then call it
B Call the function first, then define it

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1def greet(): ADefines a function named greet. Its code does not run yet.
2 print("Hi!") BThe colon is missing at the end of the def line. It should be def greet():
3Define the function first, then call it to make the body run. Cdef say_hello(): print("Hello")
4A function name is followed by brackets and a colon, and the body is indented. Ddef cheer(): print("Go!") cheer()
5def greet() print("Hi!") EThe function is called before it is defined, so Python gives a NameError. Define it first, then call it.
6greet() def greet(): print("Hi!") FAn indented line inside the function body that runs only when the function is called.

Definitions

def name():

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
___ greet(): print("Hi!") greet()
2.
def _____(): print("Hello!") greet()
3.
def welcome()_ print("Welcome!") welcome()
4.
def ______(): print("Hi!") say_hi()
5.
def beep(): _____("Beep!") beep()

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def greet():
B print(Hi!)
C greet()
Question 2
A def welcome()
B print("Welcome")
C welcome()
Question 3
A def cheer():
B print("Go team!"
C cheer()
D cheer()
Question 4
A def say_hi():
B Print("Hi")
C say_hi()
Question 5
A def party():
B print("Cake")
C party

Answer Key

Defining a function with def
Section A: Quick Questions
Q1
B — def greet():
Q2
A — Only when the function is called
Q3
A — def hello(): print("Hi!")
Q4
B — def say_hi():
Q5
A — Define the function first, then call it
Section B: Match the Pairs
Q1
def greet(): → Defines a function named greet. Its code does not run yet.
Q2
print("Hi!") → An indented line inside the function body that runs only when the function is called.
Q3
Define the function first, then call it to make the body run. → def cheer(): print("Go!") cheer()
Q4
A function name is followed by brackets and a colon, and the body is indented. → def say_hello(): print("Hello")
Q5
def greet() print("Hi!") → The colon is missing at the end of the def line. It should be def greet():
Q6
greet() def greet(): print("Hi!") → The function is called before it is defined, so Python gives a NameError. Define it first, then call it.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
def greet(): print("Hi!") greet()
Q2
def greet(): print("Hello!") greet()
Q3
def welcome(): print("Welcome!") welcome()
Q4
def say_hi(): print("Hi!") say_hi()
Q5
def beep(): print("Beep!") beep()
Section E: Spot the Error
Q1
B — Text inside print() must be in quote marks. It should be ' print("Hi!")'.
Q2
A — A def line must end with a colon. It should be 'def welcome():'.
Q3
B — The closing bracket is missing, so the function body has a syntax error. It should be ' print("Go team!")'.
Q4
B — Python is case-sensitive. The built-in function is print with a lowercase p, so it should be ' print("Hi")'.
Q5
C — Writing the name without brackets does not call the function, so nothing runs. It should be 'party()'.