Class 706 – Unit 2: Function Basics

Learn to write reusable functions with parameters

What Are Functions?

Functions are one of the most powerful tools in programming. They allow you to write code once and reuse it many times, which makes your programs:

  • More efficient - Less code to write and maintain
  • Easier to read - Named functions describe what the code does
  • Easier to debug - Fix problems in one place instead of many
  • More flexible - Use parameters to customize behavior
Key Concept: If you find yourself copying and pasting code, you probably need a function!

Without Functions (Repeated Code)

# Draw first flower
Circle(100, 300, 20, fill='red')
Circle(80, 280, 15, fill='pink')
Circle(120, 280, 15, fill='pink')
Circle(80, 320, 15, fill='pink')
Circle(120, 320, 15, fill='pink')
Line(100, 320, 100, 380, fill='green')

# Draw second flower
Circle(200, 300, 20, fill='red')
Circle(180, 280, 15, fill='pink')
Circle(220, 280, 15, fill='pink')
Circle(180, 320, 15, fill='pink')
Circle(220, 320, 15, fill='pink')
Line(200, 320, 200, 380, fill='green')

# Draw third flower
Circle(300, 300, 20, fill='red')
Circle(280, 280, 15, fill='pink')
Circle(320, 280, 15, fill='pink')
Circle(280, 320, 15, fill='pink')
Circle(320, 320, 15, fill='pink')
Line(300, 320, 300, 380, fill='green')

21 lines - Same code copied 3 times!

With Functions (Reusable Code)

# Define the function once
def drawFlower(x, y):
    Circle(x, y, 20, fill='red')
    Circle(x-20, y-20, 15, fill='pink')
    Circle(x+20, y-20, 15, fill='pink')
    Circle(x-20, y+20, 15, fill='pink')
    Circle(x+20, y+20, 15, fill='pink')
    Line(x, y+20, x, y+80, fill='green')

# Call the function 3 times
drawFlower(100, 300)
drawFlower(200, 300)
drawFlower(300, 300)

12 lines - Write once, use many times!

Today's Learning Objectives

  • Understand what functions are and why they make programming more efficient
  • Write function definitions with clear, descriptive names
  • Call functions to execute their code
  • Use parameters to pass information into functions
  • Understand and use test cases to verify function behavior
  • Convert repeated code into reusable functions
  • Debug common function errors (incorrect parameters, missing definitions)

Lesson Agenda (90 minutes)

  • Introduction (5 min): What are functions and why do we need them?
  • Direct Instruction (15 min): Function syntax, parameters, and calling functions
  • Teacher Demo (15 min): Live coding - converting repeated drawing code into a reusable function
  • Guided Practice (10 min): Work through CMU Function Basics lesson together
  • Independent Practice (40 min): Complete CMU exercises on functions, parameters, and debugging
  • Wrap-Up (5 min): Review key concepts and assign homework

Key Programming Concepts

Function Definition

Creating a function that can be used later:

def drawStar(x, y):
    Star(x, y, 30, 5, fill='gold')
    # This function draws a star at position (x, y)

Function Call

Using the function you created:

drawStar(100, 200)  # Draws a star at (100, 200)
drawStar(300, 150)  # Draws another star at (300, 150)

Parameters

Parameters are variables that let you pass information into a function. They make functions flexible and reusable for different situations.

Function Definition

def drawCircle(x, y, size):
Circle(x, y, size, fill='blue')

Function Call

drawCircle(100, 200, 50)

# Draws a circle at (100, 200) with size 50

Color Key

x parameter → receives 100
y parameter → receives 200
size parameter → receives 50

Test Cases

Test cases help you verify that your function works correctly with different inputs. In CMU CS Academy, you'll use test cases to check your work before submitting.

Teaching Strategy

The "Repeated Code Problem"

Start by showing students code that draws the same shape multiple times with only slight variations. Ask: "What's inefficient about this code?"

Live Coding Demo

Show students how to:

  1. Identify repeated code patterns
  2. Create a function definition with a clear name
  3. Add parameters for the parts that change
  4. Replace repeated code with function calls
  5. Test the function with different arguments

Suggested Demo: Draw three different snowmen by writing the code three times, then refactor into a drawSnowman(x, y) function.

What You Must Complete Today

During today's 90-minute class, you should finish:

  • CMU CS Academy: Function Basics lesson - Learn to define and call functions
  • CMU CS Academy: Debugging lesson - Practice fixing function errors
  • Start practice problems - Begin assigned exercises (unfinished work becomes homework)

Remember: Programming requires regular practice. Use your class time productively!

Common Mistakes to Watch For

Calling a function before defining it
Functions must be defined before they are called. Always write your def statements at the top of your program.
Wrong number of parameters
If a function expects 2 parameters, you must pass exactly 2 arguments when calling it.
Forgetting the parentheses
To call a function, you need parentheses: drawStar(100, 200) not drawStar
Indentation errors
The code inside a function must be indented. Python uses indentation to know what belongs to the function.

Discussion Questions

Use these to check student understanding during or after the lesson:

  • Why would a programmer choose to use a function instead of copying code?
  • What is the difference between defining a function and calling a function?
  • How do parameters make functions more flexible?
  • When you see the same code repeated multiple times, what should you do?

Homework

Complete any unfinished CMU CS Academy exercises from today's lesson.

Expected time: 30-60 minutes. Functions are a foundational concept - make sure you understand them before moving forward!

Why This Matters

Functions are one of the most important programming concepts you'll learn.

  • Functions make code more efficient and easier to understand
  • You'll use functions in every program you write from now on
  • The AP Create Performance Task requires you to write and use functions
  • Professional programmers use functions to organize complex programs

Looking Ahead

Next class (Lesson 707): We'll extend what you learned today by adding multiple parameters to functions and practicing structured design with more complex drawing functions.

Unit 2 Journey: Functions → Mouse Events → Variables → Conditionals → Creative Task