Skip to content

Latest commit

 

History

History
79 lines (55 loc) · 2.91 KB

File metadata and controls

79 lines (55 loc) · 2.91 KB

Lesson 3.02: User-Defined Functions

Learning Objectives

Students will be able to...

  • Define and identify:
    • abstraction
    • def (new Python keyword)
  • Create functions
  • Create a function contract0

Materials/Preparation

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

Abstraction

  • Abstraction is managing the complexity of a program by removing details and pushing them down to a lower level.
  • Ask students to brainstorm why a function might be useful in programming.
    • Less repeated code.
    • Breaking the problem up into smaller pieces and solving each piece.

Demonstration

  • 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.

Function Contracts

  • Introduce the concept of a function contract using #, which adds a comment (non-executed line of code).
  • The function contract should
    • Specify the name of the function.
    • Explain the purpose of the function.
    • List what arguments it takes in as input, and the types of those arguments.
    • Specify the output return type.

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.