Education digital-technologies year-9
Why Use Functions?
An explanation of what functions solve and how they improve structure, reuse, and readability in the Pet Services project.
This page introduces functions as a design tool, not just a syntax trick. It explains the problem functions solve before asking students to refactor.
Project Navigation
LESSON • FUNCTIONS (INTRODUCTION)
🧠 Functions: One Job, One Name
A function is a named block of code that does one job. Functions help you avoid repeating yourself and keep your program easier to change.
✅ Today you will learn to…
• Write a function using
• Use parameters (inputs)
• Use return (outputs)
• Explain the difference between parameter and argument
• Choose return vs print correctly
def• Use parameters (inputs)
• Use return (outputs)
• Explain the difference between parameter and argument
• Choose return vs print correctly
🎯 Success criteria
Your code uses a function that returns a value, and your main program stores that returned value in a variable.
⚠️ One rule
Your function must do one job. If it does two jobs, split it into two functions.
📌 What is feature creep (and why it ruins good code)?
Feature creep is when you keep adding extra features that were not part of the original plan.
Why it’s bad during planning
You can’t finish your decision tree because the rules keep growing. You stop planning properly and start guessing.
Why it’s bad during coding (production)
You change rules mid-build, patch random fixes, and your program becomes messy. It might “work”, but it’s hard to explain and hard to debug.
Unit rule: If it isn’t in the contract, it doesn’t exist. Clean plan → clean code.
1️⃣ What is a function?
Definition (keep this in your head)
A function is a named block of code that does one job.
The simple model
input → processing → output
Example: input = dog age, processing = apply age rules, output = updated cost
Why we use functions
Functions stop you from repeating the same logic in multiple places. That matters because when a rule changes, you want to update it once, not hunt it down in 3 different spots.
Clean code isn’t magic. It’s the result of clean thinking and tidy structure.
2️⃣ Function anatomy (what the parts mean)
def add_gst(amount):
# parameter = amount (input placeholder)
total = amount * 1.10
return total # return value = output for the program
price_with_gst = add_gst(50) # 50 is the argument
print(price_with_gst)
Parameter vs Argument (don’t mix these up)
Parameter = the name inside the function (e.g.
Argument = the actual value you pass in (e.g.
amount)Argument = the actual value you pass in (e.g.
50)Naming (make your function name do the explaining)
Use action names like:
If the name is vague, the function is probably vague too.
get_..., calc_..., apply_..., print_...If the name is vague, the function is probably vague too.
3️⃣ Return vs Print (you must understand this)
Print = show the human
def show_total(total):
print("Total is:", total)
This does not give a value back to the program. It only displays text.
Return = give the program a value
def calc_total(price, qty):
total = price * qty
return total
t = calc_total(12, 3)
print(t)
The program can store the result and use it again later.
Golden rule: If you need the result later, you must use return.
Common mistake (read if you’ve been stuck before)
If you print inside a function, you haven’t “saved” the value — you’ve just displayed it.
If you want to do something like
If you want to do something like
total_cost = ..., then you need a function that returns a number.4️⃣ Practice (you write these in Python)
For each task: complete the calculation line, then test it by calling the function and printing the returned value.
Task A — add GST
Create
add_gst(amount) that returns amount * 1.10.def add_gst(amount):
total = 0 # replace 0 with the correct calculation
return total
Task B — double a number
Create
double_number(num) that returns double the input.def double_number(num):
result = 0 # replace 0 with the correct calculation
return result
Task C — senior check (returns True/False)
Create
is_senior(age) that returns True if age ≥ 10, otherwise False.def is_senior(age):
answer = False # replace False with the correct condition
return answer
Hint
A comparison like
age >= 10 is already a True/False answer. You can return it directly (or store it in answer first).Checkpoint (don’t skip)
You should be able to point to:
• the parameter name inside your function
• the argument you passed when you tested it
• the value returned from the function
• the argument you passed when you tested it
• the value returned from the function
Show possible solutions (check after you try)
def add_gst(amount):
total = amount * 1.10
return total
def double_number(num):
result = num * 2
return result
def is_senior(age):
answer = age >= 10
return answer
5️⃣ Mini-challenge: apply_age_based_fee
This function applies the age rules to a starting cost. The age logic lives in one place, so it’s easier to change later.
Age rules (these are the only ones for this task)
• If age < 1 → apply a 10% discount (multiply by 0.9)
• If age ≥ 10 → add a $5 fee
• If age ≥ 10 → add a $5 fee
Starter code (you complete the TODO lines)
def apply_age_based_fee(base_cost, dog_age):
# base_cost = starting cost (number)
# dog_age = age in years (number)
total = base_cost
# TODO: if age < 1 apply 10% discount (total * 0.9)
# TODO: if age ≥ 10 add $5
return total
Test cases (use these to prove it works)
Write calls like this and check the output:
print(apply_age_based_fee(50, 0)) # puppy: expect 45.0 print(apply_age_based_fee(50, 5)) # normal: expect 50 print(apply_age_based_fee(50, 10)) # senior: expect 55
If your result is wrong, fix the function (do not hack the test).
Show a possible solution (check after you try)
def apply_age_based_fee(base_cost, dog_age):
total = base_cost
if dog_age < 1:
total = total * 0.9
if dog_age >= 10:
total = total + 5
return total
✅ Exit check (before you pack up)
Answer these clearly
1) What is a function?
2) What is a parameter? What is an argument?
3) When do you use return instead of print?
2) What is a parameter? What is an argument?
3) When do you use return instead of print?
Quick self-check
You can point to your code and show:
• where the function starts (
• what it returns (
• how you tested it (a function call + print)
def)• what it returns (
return)• how you tested it (a function call + print)
If you can’t explain what your function does in one sentence, it probably does too much.