Education digital-technologies year-9
Chapter 9 - Functions
A lesson on reducing repetition and structuring code into reusable functions.
Chapter 9 focuses on organising repeated behaviour into functions so the program becomes easier to understand and extend.
Chapter Navigation
Chapter 9 — Teaching the console new skills with functions
EXPLORER · v9 · organising code with functions
The console works. But look closely.
Your console now loops, collects reports, stores them in a list, and prints them at the end. Functionally, it does everything we have asked it to do so far.
However, if you scan through the code, you may notice that some parts feel repetitive. The banner prints every time. The report formatting logic is grouped together. The overall structure is starting to stretch downward.
As programs grow, this becomes harder to manage. Even when the program works, it can become difficult to read and difficult to improve.
However, if you scan through the code, you may notice that some parts feel repetitive. The banner prints every time. The report formatting logic is grouped together. The overall structure is starting to stretch downward.
As programs grow, this becomes harder to manage. Even when the program works, it can become difficult to read and difficult to improve.
Think about real-world tools
Imagine you had to cook dinner, but instead of having named tools like “knife” or “oven,” you just had a huge pile of equipment in one place. Every time you wanted to chop something, you would search through the pile.
That would work, but it would be inefficient and frustrating.
Functions are how we name and group pieces of behaviour so we can reuse them easily.
That would work, but it would be inefficient and frustrating.
Functions are how we name and group pieces of behaviour so we can reuse them easily.
What a function really is
A function is a named block of code that performs a specific task.
Instead of writing the same instructions again and again, you place them inside a function and give that block a name. Whenever you need that behaviour, you call the function by its name.
This does not change what the program can do. It changes how clearly the program is organised.
Instead of writing the same instructions again and again, you place them inside a function and give that block a name. Whenever you need that behaviour, you call the function by its name.
This does not change what the program can do. It changes how clearly the program is organised.
How functions are written
In Python, a function begins with the keyword
After
The function does nothing until it is called.
def, which stands for define. After
def comes the function name, followed by parentheses and a colon. The indented lines underneath belong to that function. The function does nothing until it is called.
EXPLORER v9 (run this)
This version reorganises the console using functions. Notice that the behaviour has not changed. The difference is in how the code is structured.
"""
EXPLORER v9 — Chapter 9
This version uses functions to organise behaviour.
"""
def print_banner():
print("=" * 44)
print(" EXPLORER CONSOLE ")
print("=" * 44)
def create_report():
mission_location = input("Enter mission location: ")
mission_day = int(input("Enter mission day (whole number): "))
temperature_c = float(input("Enter temperature (°C): "))
report_summary = (
"Location: " + mission_location +
" | Day: " + str(mission_day) +
" | Temp: " + str(temperature_c) + " °C"
)
return report_summary
# List to store reports
reports = []
keep_running = True
while keep_running:
print_banner()
report = create_report()
reports.append(report)
print()
print("Report logged.")
print()
user_choice = input("Log another report? (yes/no): ")
if user_choice.lower() != "yes":
keep_running = False
print()
print("All logged reports:")
print("-" * 44)
for report in reports:
print(report)
print("-" * 44)
print("Console shutting down.")
What changed and why it matters
The console still behaves exactly as it did before. It loops, collects reports, stores them, and prints them at the end.
The difference is that parts of the program now have clear names and clear purposes. The banner printing lives in one place. The report creation lives in one place. The main loop reads more like a set of instructions than a long list of details.
As programs grow larger, this kind of structure becomes essential. Functions are how programmers manage complexity without losing control.
The difference is that parts of the program now have clear names and clear purposes. The banner printing lives in one place. The report creation lives in one place. The main loop reads more like a set of instructions than a long list of details.
As programs grow larger, this kind of structure becomes essential. Functions are how programmers manage complexity without losing control.