KS3 · Computer Science
Functions in Python
Your game works out a score in five places, and the code has a bug. Fix it five times? Or write it once, give it a name, 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 simply go from top to bottom.
Before you press Next: line 1 says def. Do you think lines 2 and 3 run straight away?
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 line 1 and learns that there is a function called area. It does NOT run lines 2 and 3 yet. Defining a function is not the same as running it.
Step with Next. Watch the highlight jump into area and back, and watch the local variables appear and vanish.
Sort them out
Built-in or user-defined? Function or procedure?
Pick each subroutine, then pick the group it belongs in.
Still to sort
Built-in function (0)
Python provides it, ready to use.
Where the line is: Built-in or user-defined is about who wrote it. You don't need to write any def for these.
User-defined function (0)
The programmer wrote it with def, and it returns a value.
Where the line is: Look for a return line: that is what makes it a function rather than a procedure.
User-defined procedure (0)
The programmer wrote it with def, and it does not return a value.
Where the line is: A procedure can still print. Printing puts text on the screen, but it does not hand a value back to the line that called it.
Two questions decide where each one goes. Who wrote it: Python or the programmer? And if the programmer wrote it, does it return a value?
Predict, then check
This is the real reason programmers bother with subroutines.
A game works out a player's score in five different places. You discover 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 is one function that is 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 task and can be called from other parts of a program.
- A function returns a value; a procedure does not.
- In Python you define a subroutine with def, its identifier, brackets holding any parameters, and a colon, followed by an indented body.
- Defining a subroutine does not run it. It only runs when it is called, and then the program carries on from where it was called.
- Parameters receive values in the definition; arguments are the values passed in the call.
- return sends a value back to the calling line and ends the function. Variables made inside a subroutine are local and vanish when it finishes.
The big picture
A subroutine is a named block of code that does one job. In Python you define it with def, but it only runs when it is called: the arguments in the call land in the parameters, return sends a value back and ends it, and anything made inside is local, so it vanishes afterwards. A subroutine that returns a value is a function; one that doesn't is a procedure. Python has built-in functions, you can write your own, and splitting a program into subroutines makes it modular.
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
Reading a program strictly top to bottom and thinking a function's body runs when Python reaches the def line. It doesn't: Python only notes that the function exists. The body runs when it is called, then the program returns to the calling line.
Memory hook
The def is the recipe; the call is the cooking. Writing a recipe down feeds nobody. Nothing happens until someone actually cooks it, and when they do, the dish (the return value) goes back to whoever asked for it.
Check yourself
Cover the page. Write a function add_ten that returns its parameter plus 10, then one line that calls it with 7 and stores the answer in total. total should hold 17.
Flashcards
(15)What is a subroutine?
What is the difference between a function and a procedure?
How do you write the definition of a Python subroutine?
Does writing a def run the code inside it?
How do you call a subroutine?
When a subroutine finishes, where does the program carry on from?
Parameter vs argument?
What two things does return do?
Name three things you can do with a value a function returns.
What is a local variable?
Why is it useful that variables inside a subroutine are local?
Name four built-in Python functions.
Built-in function vs user-defined function?
What makes a program modular?
Give three benefits of using subroutines.
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
- Abstraction in computational thinking
- Adding binary numbers
- Binary to denary conversion
- Boolean logic: AND, OR, NOT
- Bubble sort
- Building truth tables
- Client-server vs peer-to-peer
- Collecting and recording data
- Comparing sorting algorithms
- Compressing data
- Creating a 3D animation
- Decomposition: splitting problems up
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 1 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.