Python: procedures and functions

Big programs are built from small, named blocks of code called subprograms. Write a block once, then use it as often as you like.

Practise this lesson free (6 questions)

More tools

Defining a subprogram

Use def, a name, brackets and a colon. The indented lines are the body.
def greet(name):
    print("Hi " + name)

greet("Zara")
Output: Hi Zara
name is a parameter: a value passed in when the subprogram is called. "Zara" is the argument.

Procedures and functions

A procedure carries out a task but does not send a value back (greet above just prints).
A function sends a value back with return.
def area(width, height):
    return width * height

print(area(4, 5))
Output: 20

Why use subprograms?

Reuse: write the code once, call it many times.
Decomposition: each subprogram solves one small part of the problem.
Testing: you can test each part on its own.
Teamwork: different people can write different subprograms.
Programs built this way are called modular.
Worked example

What does this print?
def double(n):
    return n * 2

x = double(3)
print(double(x))

  1. double(3) returns 6, so x = 6.
  2. double(6) returns 12.

Answer: 12

Key idea

Define subprograms with def. Parameters receive values. A function returns a value; a procedure does not. Subprograms make code reusable, easier to test and easier to share in a team.

Key words
AND
A Boolean operator that gives True (1) only when both inputs are True.
Function
A subprogram that carries out a task and returns a value to the code that called it.
Procedure
A subprogram that carries out a task but does not return a value.
NOT
A Boolean operator that flips its input: True becomes False and False becomes True.
Output
Information sent out from a computer, for example to a screen, speaker or printer.
Parameter
A variable in a subprogram's definition that receives a value when the subprogram is called.

All KS3 Computing key terms and definitions →

Quick check

Try two quick questions

See if it has sunk in. Answers are marked straight away.

Extra practice

2 more questions

Try each one, then tap to check.

  1. What does this print?
    def area(width, height):
    return width * height

    print(area(5, 6))

    Show the answer

    Answer: 30

    5 × 6 = 30.

  2. What does this print?
    def double(n):
    return n * 2

    x = double(4)
    print(double(x))

    Show the answer

    Answer: 16

    double(4) = 8, then double(8) = 16.

Check you have got it

Answer 6 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.

Video for this lesson coming soon. Subscribe on YouTube

Sources and references

Facts checked against the sources below, 27 October 2026. Last updated 2 October 2026. Spotted a mistake? Tell us and we will fix it.

Related lessons