Lesson 3.02: User-Defined Functions

Learning Objectives

Students will be able to...

  • Define and identify: abstraction, def
  • Create functions

Materials/Preparation

  • Do Now
  • Lab - Birthday Song & Random Cards
  • Associated Reading - section 3.2 of Book
  • Read through the do now, lesson, and lab so that you are familiar with the requirements and can assist students.

Pacing Guide

Duration Description
5 Minutes Do Now
10 Minutes Lesson
35 Minutes Lab
5 Minutes Debrief

Instructor's Notes

  1. Do Now
    • Students should take 5 minutes to follow the instructions on the Do Now in order to create/manipulate a user-defined function.
  2. Lesson
    • Ask students to brainstorm why, in general, a function might be useful in programming.
      • Abstraction: managing the complexity of a program by removing details and pushing them down to a lower level
      • Less repeated code.
      • Breaking the problem up into smaller pieces and solving each piece
    • Demonstrate to students how you create a function using def, calling out the syntax and where arguments would go.
      • Ask students how they would call your example function.
    • Have students practice making a function that takes two arguments, adds them together, and returns the sum.
    • Introduce the concept of a function contract using #, which adds a comment (non-executed line of code)
      • The function contract should explain what the function will do, what arguments it takes in, and the types of those arguments
  3. Lab
    • Practice making a function that will take in a name as an argument and output the 'happy birthday song' to that name.
    • Create a function that randomly selects 5 cards from a deck of cards (repeating allowed).
  4. Debrief
    • Check student progress and completion of the lab, wrap up by taking any final questions.

Accommodation/Differentiation

If students are moving quickly, they could go back and use functions to improve an old project.