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 functiondefadd_numbers(a, b): # a and b are parameters total = a + breturn total # returns a value# Calling the functionresult = add_numbers(5, 3) # 5 and 3 are argumentsprint(result) # Output: 8
Procedures (no return value)
# A procedure — does a task but returns nothingdefgreet(name):print("Hello, " + name + "!")greet("Alice") # Output: Hello, Alice!greet("Bob") # Output: Hello, Bob!
Parameters and Arguments
Term
Definition
Example
Parameter
Variable in the function definition that receives a value when called
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 variabledefmy_function(): y = 20# local variable — only exists inside this functionprint(x) # can access global xprint(y) # can access local ymy_function()print(x) # works — x is global# print(y) # ERROR! y doesn't exist outside the function
Benefits of Using Subroutines
Benefit
Explanation
Avoid code repetition (DRY)
Write code once, call it many times
Easier to read
Meaningful function names make code self-documenting
Easier to test
Can test each function independently
Easier to maintain
Change code in one place, not everywhere it's used
Decomposition
Break 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
✅ Notes completed!
▶
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!
Term
Definition
🎯
Mini Test — Subroutines
Timed exam-style test — 10 minutes.
⏱10:00
Section A — Multiple Choice [5 marks]
Q1What keyword is used to define a subroutine in Python?[1]
Q2What is the key difference between a function and a procedure?[1]
Q3In def greet(name): — what is 'name'?[1]
Q4What happens if you try to use a local variable outside its function?[1]
Q5What does a function return if there is no return statement?[1]
Section B — Short Answer
Q6Write a Python function called max_of_two that takes two numbers as parameters and returns the larger one.[3]
Mark schemedef max_of_two(a, b): [1]; if a > b: [1]; return a [1]; else: return b [1]. (Accept: return a if a > b else b — full marks. Accept max(a,b) as a valid alternative for 3 marks.)
Q7Explain, using an example, why decomposition using subroutines makes programs easier to maintain.[2]
Mark schemeIf a subroutine needs updating, the change only needs to be made in one place [1]; rather than finding and changing the same code wherever it appears [1]; e.g. a function to calculate VAT — if the VAT rate changes, you update only the function, not every place in the code that calculates VAT [1].