Why Functions Matter in HKDSE
Functions appear in over 90% of HKDSE ICT Paper 1 questions. Paper 1B almost always has a multi-part question requiring you to define and call functions. Master this topic and you unlock 15-20 marks.
Anatomy of a Function
def function_name(parameter1, parameter2):
# code here
return value
def— the keyword that defines a functionfunction_name— your chosen name (snake_case by convention)- Parameters — inputs the function receives
return— the value sent back to the caller
Simple Example
def square(n):
return n * n
print(square(5)) # 25
print(square(10)) # 100
Multiple Parameters
def add(a, b):
return a + b
def bmi(weight, height):
return weight / (height ** 2)
print(add(3, 4)) # 7
print(bmi(60, 1.70)) # 20.76...
Default Parameters
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
print(greet("Chan")) # "Hello, Chan!"
print(greet("Lee", "Good morning")) # "Good morning, Lee!"
Multiple Return Values (Common in HKDSE)
def min_max(lst):
return min(lst), max(lst)
lowest, highest = min_max([3, 1, 4, 1, 5, 9])
print(lowest, highest) # 1 9
Functions Without Return
A function without return returns None. Common for functions that just print:
def print_header():
print("=" * 30)
print("STUDENT REPORT")
print("=" * 30)
print_header()
Scope: The Trickiest Topic
Variables inside a function are local — they don't exist outside.
def compute():
x = 10 # local to compute()
return x
compute()
print(x) # NameError — x doesn't exist here
Global Variables
total = 0
def add_score(n):
global total # needed to modify
total += n
add_score(5)
add_score(3)
print(total) # 8
Common HKDSE Question Pattern
"Write a function calculate_grade(score) that returns the grade letter based on the following scheme..."
def calculate_grade(score):
if score >= 80: return 'A'
elif score >= 65: return 'B'
elif score >= 50: return 'C'
elif score >= 35: return 'D'
else: return 'F'
# Use in main program
scores = [78, 92, 45, 67]
for s in scores:
print(f"{s}: {calculate_grade(s)}")
Functions Calling Other Functions
Paper 1B often requires function composition:
def is_vowel(char):
return char.lower() in 'aeiou'
def count_vowels(text):
count = 0
for char in text:
if is_vowel(char):
count += 1
return count
print(count_vowels("HKDSE ICT")) # 2
Testing Checklist
- Did I use
defto define the function? - Are parameters in the correct order?
- Does every code path have a
return(if needed)? - Am I actually calling the function somewhere?
- Are variable names matching what the question specifies?
Build Confidence with Function Practice
PyForm's Special Task generates HKDSE-style function questions at Easy, Hard, and Extreme levels.
Practise Functions Free →