← Python Coding Track

Module 04FoundationWeeks 1–2

Functions

01

Learning outcomes

  • Write reusable, organized code
02

Core concepts

Defining functionsParametersReturn valuesScope
03

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.

04

Guided labs

Build reusable functions

  1. 1.Create functions.py
  2. 2.Write celsius_to_fahrenheit(celsius) returning (C × 9/5) + 32
  3. 3.Write calculate_repair_cost(parts_cost, labor_cost) returning the total
  4. 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. 1.Use a function to collect and validate input (name, age, location, interests) — reject empty names and unrealistic ages
  2. 2.Use a loop to let the user re-enter any field that fails validation
  3. 3.Use a function to format and return a clean profile summary
  4. 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.

05

Knowledge Check

Question 1 of 6

What's the difference between defining and calling a function?

06

What you read

  • Why functions prevent repeated code
  • How parameters pass data in
  • How return values pass data out
  • Local vs. global scope
07

What you understand

Key takeaways

  • Functions are the building blocks of larger programs
  • Good functions do one thing well
08

Hands-on lab

Lab tasks

  1. 1.Build reusable functions for common tasks (e.g., a temperature converter)
09

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.

10

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.

11

Mark progress