2.3: Reusable Code - Parameters, Arguments & Return Values Explained
Lesson 2.3: Reusable Code - Functions
I Do: Introduce functions as “sub-recipes.” Demonstrate how to define a function, pass arguments, and return values. Explain the concept of code reusability and avoiding “DRY” (Don’t Repeat Yourself).
We Do: Collaboratively, using GitHub Copilot, we’ll refactor our previous discount script. We’ll create a function `calculate_discount(price)` that encapsulates the discount logic. Copilot will help define the function signature and internal logic.
You Do: Create two functions: one that takes two numbers and returns their sum, and another that takes a name and returns a personalized greeting. Call both functions in your script. Success criteria: Functions are defined correctly and produce expected output.
Module 2: Building Blocks - Logic, Loops, and Functions
Now that you know how to store information, let’s teach Python how to make decisions, repeat tasks, and organize its thoughts. This is where your code starts to become truly smart and efficient.
Rationale
Technical Friction Point: Understanding control flow (when to do what) and the concept of code reusability (functions) can be challenging. Beginners often write repetitive code or struggle with the logical structure of conditional statements and loops.
Extended Analogy: Think of a detailed recipe book. Conditional statements (if/else) are like “If the sauce is too thick, add a splash of water, otherwise, continue simmering.” Loops (for/while) are “Stir the mixture until it thickens” or “Add one egg at a time, 6 times.” Functions are like “sub-recipes” or “pre-made components” (e.g., “Prepare the béchamel sauce”) that you can call upon whenever needed without rewriting all the steps.
Analogy Bridge: This analogy directly translates to algorithmic thinking. Conditionals guide the program’s decision-making path. Loops automate repetitive tasks, saving effort and reducing errors. Functions promote modularity, allowing complex problems to be broken down into manageable, reusable pieces, a cornerstone of efficient software design. This module introduces the first mandatory use of AI augmentation.

