KS3 · Computer Science
Functions in Python
Your game's score calculation is copied into five places, and it has a bug. Fix it five times, or write it once and call it?
Python · Subroutines
Follow the call: into the function and back
Press Next to run this program one line at a time. Keep your eye on the highlighted line. It does not just go top to bottom.
Before you press Next: line 1 starts with def. Do lines 2 and 3 run straight away? Make a guess, then step through.
1 def area(length, width):2 space = length * width3 return space4 kitchen = area(4, 3)5 hall = area(5, 2)6 print(kitchen + hall)
Variables
Output
Step 1: Python reads the def and now knows there's a function called area. But lines 2 and 3 don't run yet. Defining a function isn't running it.
Watch two things: the highlighted line jumping into area and back, and the local variables appearing during each call and then going.
Sort them out
Built-in or user-defined? Function or procedure?
Put each one in its box. Then read why.
Still to sort
Built-in function (0)
Comes with Python. Nobody writes it with def.
Where the line is: Built-in or user-defined is about who wrote it, not about what it does.
User-defined function (returns a value) (0)
Written with def, and it sends a value back with return.
Where the line is: This is what people usually mean by a function: it returns a value to the line that called it.
User-defined procedure (no return value) (0)
Written with def, and it does its job without returning a value.
Where the line is: This is what's often called a procedure: a subroutine that does not return a value.
First ask: who wrote it, Python or the programmer? If the programmer wrote it, ask: does it send a value back with return?
Predict, then check
This is the real reason programmers bother with subroutines.
A game works out a player's score in five different places, and the score calculation has a mistake in it. How many places do you have to fix if (a) the calculation is typed out five times, or (b) it's one function that's called five times?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Write a job once, give it a name, and run it whenever you need it.
What you need to know
- A subroutine is a named block of code that does one specific job. You run it, or call it, whenever that job is needed.
- In Python you define one with def, its name, brackets for any parameters, and a colon. The indented lines underneath are its body.
Have a goJake types def greet(name) and Python refuses to run it. He insists his code is perfect, letter for letter. What has he forgotten?
The colon at the end: def greet(name):
The name and brackets are right, but every def line must end with a colon to tell Python the indented body comes next.
- Defining isn't running. The body runs only when you call the subroutine, and afterwards the program carries on from where it was called.
Have a goA whole program is just def hello(): with print("Hi") indented underneath. Nothing else. How many times does Hi appear?
Zero times.
The def only creates hello. Nothing ever calls hello(), so its body never runs, however long you wait.
- Parameters are the variables in the definition. Arguments are the values passed in the call.
- return sends a value back to the calling line, where you can store it, print it or use it in a sum. It also ends the function.
Have a godef double(n): return n * 2. Then total = double(4) + double(1). What does total hold, and does anything appear on screen?
total holds 10. Nothing appears on screen.
Each call sends its value back (8, then 2) to the line that called it, which adds them. return isn't print, so nothing is shown.
- print is not return: printing puts text on the screen, while return sends a value back to the line that called the subroutine.
- Variables created inside a subroutine are local. They exist only while it runs, and the rest of the program can't use them.
- A subroutine that returns a value is usually called a function; one that doesn't is often called a procedure.
- Python has built-in functions like print(), input(), len() and int(). The ones you write yourself with def are user-defined.
- Splitting a problem into subroutines makes a program modular: code is reused, not repeated, and is easier to read, test, debug and change.
The big picture
A subroutine is a named block of code you define once with def and call whenever its job is needed; defining it doesn't run it. Arguments in the call fill the parameters, return sends a value back and ends the function, and local variables last only while it runs. Built-in or written by you, subroutines make a program modular: easier to read, test, debug and change.
Key points
Worked example
Problem
What does this program print? def triple(n): return n * 3 x = triple(2) print(triple(x) + 1)
⚠ Watch out
Mixing up return and print. print puts text on the screen. return sends a value back to the line that called the function, and shows nothing unless that line prints it.
Memory hook
def builds the machine; a call switches it on. Arguments go in, return sends one thing back to the line that called it.
Check yourself
Write add_ten(n) so it returns n + 10, and call it with the argument 5. Then say what the calling line must do for the returned value to appear on the screen.
Flashcards
(13)What does def do in Python?
What makes a subroutine's body actually run?
Where does the program go when a called subroutine finishes?
Parameter or argument: which is which?
What does a call like area(4, 3) pass in?
What two things does return do?
What can the calling line do with a returned value?
Return or print: which sends a value back to the calling line?
What is a local variable?
Function or procedure?
Built-in or user-defined function?
What makes a program modular?
Why is a bug in a subroutine quicker to fix than a bug in copied code?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore KS3 Computer Science topics
How this lesson was checked. This KS3 Computer Sciencelesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 27 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.