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

1 / 12

Step with Next. Watch the highlight jump into area and back, and watch the local variables appear and vanish.

Step 1 of 12: 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..

Watch out: return is not print. return hands a value back to the line that called the function. Nothing appears on screen unless that line prints it, like line 6 does.

Parameter or argument?

ParametervsArgument

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

Focus

Where you write it

Parameter

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

Argument

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

The insight

Quick test: if the line starts with def, the names in the brackets are parameters. Anywhere else, the values in the brackets are arguments.

What it is

Parameter

A variable, waiting to receive a value

Argument

The actual value that gets passed in

From one call to the next

Parameter

Stays the same: name is always name

Argument

Can be different every time: "Aisha", then "Ben"

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 lines that start with #.
  2. missing step
Which line is step 2?

Spot 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?

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.

8 of 8 still to sort.

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

1Subroutine: a block of code with an identifier (its name) that carries out one specific task and can be called whenever that task is needed.
2Function or procedure: a subroutine that returns a value is usually called a function; one that does not is often called a procedure.
3Defining: def identifier(parameters): followed by an indented body. Writing the def does not run the code.
4Calling: write the identifier followed by brackets holding the arguments, for example area(4, 3). When the subroutine finishes, the program carries on from the line that called it.
5Parameters are the variables in the definition that receive values; arguments are the actual values passed in, so the same subroutine can work on different data each time.
6return sends a value back to the calling line, where it can be stored in a variable, printed or used in an expression. return also ends the function.
7Local variables exist only while their subroutine runs and cannot be used by the rest of the program, which keeps subroutines independent of each other.
8Python provides built-in functions such as print(), input(), len() and int(); programmers write their own user-defined functions.
9Modular programs: decompose a problem into smaller parts, write each as a subroutine, and reuse code instead of repeating it, so programs are 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

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?
A named block of code that carries out one specific task and can be called from other parts of a program whenever that task is needed.
What is the difference between a function and a procedure?
A function returns a value. A procedure does not.
How do you write the definition of a Python subroutine?
def, the identifier, brackets holding any parameters, and a colon, for example def area(length, width): followed by the body, indented underneath.
Does writing a def run the code inside it?
No. The body only runs when the subroutine is called.
How do you call a subroutine?
Write its identifier followed by brackets containing any arguments, for example greet("Aisha").
When a subroutine finishes, where does the program carry on from?
From the point where the subroutine was called.
Parameter vs argument?
A parameter is a variable in the definition that receives a value. An argument is the actual value passed in when the subroutine is called.
What two things does return do?
It sends a value back to the point where the function was called, and it ends the function.
Name three things you can do with a value a function returns.
Store it in a variable, print it, or use it in an expression.
What is a local variable?
A variable created inside a subroutine. It exists only while the subroutine runs and cannot be used by the rest of the program.
Why is it useful that variables inside a subroutine are local?
It keeps subroutines independent of each other.
Name four built-in Python functions.
print(), input(), len() and int().
Built-in function vs user-defined function?
Python provides built-in functions. A user-defined function is one the programmer writes themselves with def.
What makes a program modular?
The problem is decomposed into smaller parts, and each part is written as its own subroutine.
Give three benefits of using subroutines.
Code is reused instead of repeated, and the program is easier to read, test, debug and change.

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 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.