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 * width
3 return space
4 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.

1 / 12

Watch two things: the highlighted line jumping into area and back, and the local variables appearing during each call and then going.

Step 1 of 12: 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 out: return is not print. return sends a value back to the line that called the function; print puts text on the screen. A returned value only shows up if something prints it.

Parameter or argument?

ParametervsArgument

You just watched length and width catch 4 and 3. Those two roles have names, and they're very easy to mix up.

Focus

Where you write it

Parameter

In the definition, in the brackets after the name: def greet(name):

Argument

In the call, in the brackets: greet("Aisha")

The insight

Quick test: on the def line, the names in the brackets are parameters. Anywhere else, what's in the brackets is an argument.

What it is

Parameter

A variable that receives a value when the function is called

Argument

The actual value passed in: typed straight in, like 4, or worked out, like x + 1

From one call to the next

Parameter

Stays the same name in the definition every time

Argument

Can be different every call

In greet("Aisha")

Parameter

name

Argument

"Aisha"

Your turn · Writing a function

Build it line by line

A cinema charges £8 for an adult ticket and £5 for a child ticket. Write a function called ticket_price that takes the number of adults and the number of children and returns the total cost. Then use it to find the cost for 2 adults and 3 children.

  1. # adult ticket £8, child ticket £5A comment to remind us of the prices. Python ignores anything after a #.
  2. missing step
Which line is step 2?

Find the bug · Local variables

Why won't it print 10?

Amir wants this program to print 10. Instead, Python stops with an error. Which line goes wrong?

Amir's program — which line goes wrong?

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.

8 of 8 still to sort.

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

1def creates a subroutine and names it; nothing in the body runs until it is called.
2A call jumps into the body with the arguments in the parameters, then comes back to the calling line.
3return sends one value back and ends the function; print only shows text on the screen.
4Local variables last only while the subroutine runs; return the value if the rest of the program needs it.
5Built-in functions come with Python; user-defined ones are written by the programmer.
6Subroutines make programs modular: decomposed, reused, and easier to read, test, debug and change.

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?
It creates a subroutine and gives it a name. It doesn't run the body.
What makes a subroutine's body actually run?
Calling it: its name followed by brackets holding any arguments, e.g. area(4, 3).
Where does the program go when a called subroutine finishes?
Back to where it was called, and it carries on from there.
Parameter or argument: which is which?
Parameter: a variable in the definition. Argument: the value passed in the call.
What does a call like area(4, 3) pass in?
The arguments 4 and 3: the actual values the subroutine works with in that call.
What two things does return do?
It sends a value back to the calling line, and it ends the function.
What can the calling line do with a returned value?
Store it in a variable, print it, or use it in an expression.
Return or print: which sends a value back to the calling line?
return does. print only puts text on the screen.
What is a local variable?
A variable created inside a subroutine. It exists only while that subroutine runs.
Function or procedure?
A subroutine that returns a value is usually called a function; one that doesn't is often called a procedure.
Built-in or user-defined function?
Built-in: comes with Python, like print(), input(), len(), int(). User-defined: written by the programmer with def.
What makes a program modular?
The problem is decomposed into smaller parts, each written as a subroutine.
Why is a bug in a subroutine quicker to fix than a bug in copied code?
You fix it once and every call uses the fixed 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 posted

More KS3 Computer Science topics

See the full KS3 Computer Science curriculum →

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.

Helpful guides for parents and students