🔒

Unlock Pro

Subscribe to access all 59 Edexcel 1CP2 lessons.

£7.99/month
or £59/year
🐍 Paper 2 · Topic 6: Programming
6.1c Subroutines: Functions & Procedures
Edexcel 1CP2 · GCSE Computer Science · ~13 min read · 🔒 Pro
Notes
──
Video
──
Slides
──
Worksheet
──
Quiz

What are Subroutines?

A subroutine is a named block of code that performs a specific task and can be called (invoked) from elsewhere in the program. Subroutines avoid repeating the same code and make programs easier to read and maintain.

In Python, subroutines are defined using def. There are two types:

  • Functions — perform a task AND return a value
  • Procedures — perform a task but do NOT return a value (Python doesn't distinguish, but Edexcel does)

Defining and Calling Functions

# Defining a function def add_numbers(a, b): # a and b are parameters total = a + b return total # returns a value # Calling the function result = add_numbers(5, 3) # 5 and 3 are arguments print(result) # Output: 8

Procedures (no return value)

# A procedure — does a task but returns nothing def greet(name): print("Hello, " + name + "!") greet("Alice") # Output: Hello, Alice! greet("Bob") # Output: Hello, Bob!

Parameters and Arguments

TermDefinitionExample
ParameterVariable in the function definition that receives a value when calleddef greet(name): — name is a parameter
ArgumentActual value passed to the function when calledgreet("Alice") — "Alice" is an argument

Functions can have multiple parameters:

def calculate_area(length, width): return length * width area = calculate_area(10, 5) print(area) # 50

Variable Scope

Scope refers to where a variable can be accessed in a program:

  • Local variable — defined inside a function; only accessible within that function
  • Global variable — defined outside all functions; accessible everywhere
x = 10 # global variable def my_function(): y = 20 # local variable — only exists inside this function print(x) # can access global x print(y) # can access local y my_function() print(x) # works — x is global # print(y) # ERROR! y doesn't exist outside the function

Benefits of Using Subroutines

BenefitExplanation
Avoid code repetition (DRY)Write code once, call it many times
Easier to readMeaningful function names make code self-documenting
Easier to testCan test each function independently
Easier to maintainChange code in one place, not everywhere it's used
DecompositionBreak large problems into smaller, manageable sub-tasks
Exam tip: Know the difference: a function returns a value (use return); a procedure performs a task but doesn't return a value. Also know the difference between parameters (in the definition) and arguments (passed when calling).
⚠️ Common Mistakes
  • Forgetting return in a function — the function returns None by default
  • Confusing parameters and arguments — parameters are in the def line; arguments are passed when calling
  • Trying to use a local variable outside the function — causes NameError
  • Calling a function before defining it — causes NameError
  • Missing parentheses () when calling a function — this doesn't call it, it just references it
Video coming soon
Click slide or press arrow keys to navigate
✍️

Worksheet — 6.1c Subroutines

8 Edexcel-style questions · instantly marked

Q1What is a subroutine? State the two types used in Edexcel pseudocode and the key difference between them.[4]
✅ Mark scheme
A subroutine is a named block of code that performs a specific task and can be called from elsewhere [1]; two types: function [1] and procedure [1]; the key difference: a function returns a value; a procedure does not return a value [1].
Q2Write a Python function called square that takes one parameter (a number) and returns its square (number × number).[3]
✅ Mark scheme
def square(number): [1]; (indented) return number * number [1] (accept ** 2); can also write: return number ** 2 [1 total]. Must include def keyword and return statement.
Q3What is the difference between a parameter and an argument? Give an example of each.[4]
✅ Mark scheme
A parameter is a variable in the function's definition that receives a value when the function is called [1]; e.g. in def greet(name): — 'name' is a parameter [1]; an argument is the actual value passed to the function when it is called [1]; e.g. greet("Alice") — "Alice" is an argument [1].
Q4A student's code: def greet(): print("Hello!") and then calls greet. State the error and correct the code.[2]
✅ Mark scheme
Error: 'greet' without parentheses does not call the function — it just references it [1]; Correct: greet() — must include parentheses to actually call/invoke the function [1].
Q5Explain what is meant by variable scope. What is the difference between a local and a global variable?[3]
✅ Mark scheme
Scope refers to the region of a program where a variable can be accessed [1]; a local variable is defined inside a function and only exists within that function [1]; a global variable is defined outside functions and can be accessed from anywhere in the program [1].
Q6Give three benefits of using subroutines (functions/procedures) in a program.[3]
✅ Mark scheme
Any three: avoids repeating code — write once, call many times [1]; makes code easier to read with meaningful names [1]; easier to test each component independently [1]; easier to maintain — change in one place [1]; supports decomposition of complex problems [1].
Q7Write a Python function called is_even that takes an integer and returns True if it is even, False if it is odd.[3]
✅ Mark scheme
def is_even(n): [1]; if n % 2 == 0: return True [1]; else: return False [1]. (Accept: return n % 2 == 0 — full 3 marks if correct and concise.)
Q8What is printed by this code? Explain your answer. x = 5; def change(): x = 10; print(x); change(); print(x)[3]
✅ Mark scheme
Prints: 10, then 5 [1]; Inside change(), x = 10 creates a new LOCAL variable x that shadows the global one [1]; After calling change(), the global x is still 5 because the local x inside the function had a different scope [1].
Topic Quiz
Q 1 of 15
You scored
out of 15
Click to reveal definition
🎉
Session complete!
TermDefinition
🎯

Mini Test — Subroutines

Timed exam-style test — 10 minutes.

← 6.1b Selection & IterationTopic 6 · PythonNext: 6.1d Advanced Loops →