Python functions: def, parameters and return

Functions let you package a block of code, give it a name, and reuse it — a required GCSE Computer Science topic and the foundation for decomposition at A Level. This unit covers defining a function with def, adding parameters, returning values with return, calling functions, and the distinction between what happens inside a function (local scope) and outside it. Every example runs in the browser so learners can trace the value flow and see what return actually does. Perfect for classroom teaching, GCSE and A Level revision, and independent or home-schooled learners.


What you'll learn

  • Define a function with def name(parameters):
  • Pass values in through parameters
  • Send a value back with return
  • Call a function and use its returned value
  • Understand the difference between local and global variables
  • Split a long program into small, well-named functions

UK GCSE topic coverage

  • Subroutines (functions)
  • Function parameters
  • Repeated function calls
  • Multiple parameters
  • Return values
  • Using return values
  • Local scope
  • Global scope
  • Functions inside iteration
  • Dry-run of function calls
  • Applied subroutines

Worked example

A reusable function that returns a value

def area_of_rectangle(width, height):
    return width * height

print(area_of_rectangle(3, 4))
print(area_of_rectangle(10, 2))

Try a full unit free with a RunPy account

Sign up free and get the whole of Unit 1 (Input, Output and Variables) with checked tasks and instant feedback, plus a selection of the exam-style questions so you can practice on real GCSE-style content before upgrading.


Lessons in this unit

Full interactive lessons with checked tasks are available to signed-in RunPy users. Unit 1 and a selection of exam-style questions are free with a RunPy account.

  1. Introducing subprograms
  2. Subprograms with parameters
  3. Calling subprograms multiple times
  4. Subprograms with multiple parameters
  5. Returning values from functions
  6. Storing returned values
  7. Local scope and variable visibility
  8. Global variables and the global keyword
  9. Calling functions in loops
  10. Tracing function calls
  11. Composite algorithm using functions

Frequently asked questions

What's the difference between print and return?
print displays a value on screen for a human to read. return sends a value back so the rest of the program can use it. GCSE questions specifically test this distinction.
Do all functions need to return something?
No. A function without an explicit return implicitly returns None. Void-style functions that just print or modify things are common and fine at GCSE.
What is scope?
Scope is the region of a program where a variable is visible. Variables created inside a function are local to that function and disappear when it returns — they don't affect the outside program.