Learning outcomes
- Write reusable, organized code
Core concepts
Lessons
Module goal
Learner understands how to package code into reusable blocks, and can write functions that accept input and return output.
Lesson 1 of 5 · lecture
Why functions exist
Step 1 of 2
If you needed to check an age in three places in a bigger program, you'd copy the same lines three times — and fix any bug three times. A function lets you write a block once, name it, and call it from anywhere.
The mindset shift
This is the move from “writing instructions” to “building tools”. Once a function exists, you only need to know what to give it and what comes back. Hiding complexity behind a clean, reusable interface is one of the most important ideas in all of programming.
Guided labs
Build reusable functions
- 1.Create functions.py
- 2.Write celsius_to_fahrenheit(celsius) returning (C × 9/5) + 32
- 3.Write calculate_repair_cost(parts_cost, labor_cost) returning the total
- 4.Call both with different values and print the results
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
def calculate_repair_cost(parts_cost, labor_cost):
total = parts_cost + labor_cost
return total
temp_result = celsius_to_fahrenheit(30)
print(f"30°C is {temp_result}°F")
repair_total = calculate_repair_cost(150, 80)
print(f"Total repair cost: {repair_total}")Foundation capstone — personal profile script
- 1.Use a function to collect and validate input (name, age, location, interests) — reject empty names and unrealistic ages
- 2.Use a loop to let the user re-enter any field that fails validation
- 3.Use a function to format and return a clean profile summary
- 4.Print the final formatted profile
def get_valid_age():
while True:
age = int(input("Enter your age: "))
if 1 <= age <= 120:
return age
else:
print("Please enter a realistic age.")
def build_profile(name, age, location, interests):
return f"Name: {name}\nAge: {age}\nLocation: {location}\nInterests: {interests}"
name = input("Enter your name: ")
age = get_valid_age()
location = input("Enter your location: ")
interests = input("Enter your interests: ")
profile = build_profile(name, age, location, interests)
print("\n--- Your Profile ---")
print(profile)Teaching point
This capstone combines Modules 1–4: variables, data types, control flow, loops and functions.
Success criteria
Both functions run correctly with different inputs, and the capstone rejects invalid age input and re-prompts until valid before printing a clean profile.
Knowledge Check
Question 1 of 6
What's the difference between defining and calling a function?
What you read
- Why functions prevent repeated code
- How parameters pass data in
- How return values pass data out
- Local vs. global scope
What you understand
Key takeaways
- — Functions are the building blocks of larger programs
- — Good functions do one thing well
Hands-on lab
Lab tasks
- 1.Build reusable functions for common tasks (e.g., a temperature converter)
Knowledge check
- Identify function inputs/outputs
- Debug a broken function
Write your answers in your own notes before moving on — explaining a concept in your own words is the fastest way to find the gaps.
Foundation capstone
Personal Profile Script
Build a Personal Profile Script: takes user input (name, age, location, interests), validates it (realistic age, non-empty fields) using control flow, and displays a formatted profile — combining variables, conditionals, loops and functions.