Subprograms: functions and procedures

Subprograms are named blocks of code you can call whenever you need them. They are how big programs stay organised.

Practise this lesson free (7 questions)

More tools

Functions and procedures

A function returns a value with return.
def area(width, height):
    return width * height

print(area(4, 5))
A procedure carries out a task but does not return a value.
def greet(name):
    print("Hello", name)

greet("Kai")
Parameters are the names in the brackets (width, height). The values passed in when it is called (4, 5) are the arguments.

Built-in and library subprograms

Python has built-in subprograms such as print(), len(), int() and input().
Library subprograms are imported, for example:
import random
dice = random.randint(1, 6) gives a whole number from 1 to 6 inclusive.

Local and global variables

A local variable is created inside a subprogram and only exists while it runs.
A global variable is created in the main program and can be read anywhere.
Using local variables avoids one part of a program accidentally changing another.
Benefits of subprograms: code is reused instead of repeated, each part can be tested on its own, programs are easier to read, and work can be shared in a team.
Worked example

What does this print?
x = 10

def change():
    x = 5
    return x

y = change()
print(x + y)

  1. x = 5 inside change() is a new local variable.
  2. The global x is still 10, and y = 5.
  3. 10 + 5

Answer: 15

Key idea

Functions return values; procedures do not. Parameters receive values passed in. Local variables exist only inside the subprogram. Subprograms make code reusable, testable and readable.

Key words
Subprogram
A named block of code that can be called: a function or a procedure.
Function
A subprogram that returns a value.
Procedure
A subprogram that carries out a task but does not return a value.
Local variable
A variable declared inside a subprogram that can only be used there.
Variable
A named store whose value can change while the program runs.
Global variable
A variable declared in the main program that can be used anywhere in it.

All GCSE and International GCSE Computer Science 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(9, 4))

    Show the answer

    Answer: 36

    width = 9 and height = 4, so it returns 9 × 4 = 36.

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

    Show the answer

    Answer: 18

    The inner call gives triple(2) = 6, then triple(6) = 18.

Check you have got it

Answer 7 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, 16 October 2026. Last updated 2 October 2026. Spotted a mistake? Tell us and we will fix it.

What do exam command words like explain and evaluate mean? →

Related lessons