SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
Edexcel 1CP2 · Topic 6 · 6.1c

Subroutines:
Functions & Procedures

def · Parameters · Return Values · Scope · Why Use Subroutines?

CSZoneEdexcel GCSE Computer Science 1CP2
Procedures (no return value)

Subroutines That Do a Task

def greet(name): # define procedure
print("Hello", name) # just does something

greet("Alice") # call it
greet("Bob")
A procedure performs a task but does not return a value back to the calling code
Parameters: values passed into the subroutine; parameters are the names in the definition, arguments are the values passed in the call
Functions (return a value)

Subroutines That Calculate

def add(a, b):
return a + b # sends result back

result = add(3, 5)
print(result) # 8

def is_even(n):
return n % 2 == 0 # returns True/False
A function uses return to send a value back — it can be stored in a variable or used in an expression
In Python, there is no separate keyword for functions vs procedures — both use def
Scope: Local vs Global

Where Variables Live

total = 100 # global variable

def calculate():
bonus = 20 # local variable
return total + bonus

print(calculate()) # 120
# print(bonus) # ERROR — bonus not defined here
Local variables: exist only inside the subroutine; destroyed when it finishes
Global variables: accessible anywhere; use global x inside a function to modify a global
Edexcel: understand why using local variables is better practice — avoids unintended side effects
Exam Practice

Have a go at this question

Edexcel-style question
Write a Python function called area that takes the length and width as parameters and returns the area of a rectangle.
3 marks
def area(length, width):
return length * width

print(area(5, 3)) # 15
Key Takeaways

What to Remember

Both functions and procedures use def in Python
Procedure: no return value; Function: uses return to send a result back
Local scope: variable only exists inside the function; global: accessible everywhere
Benefits: code reuse, easier to test/debug, improves readability, modular design