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 (printable lab document) (editable lab document)
- 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.
- Associated Readings 3.2
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
- explain the purpose
- list what arguments it takes in and the types of those arguments
- specify the 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.
Forum discussion
Lesson 3.02: User-Defined Functions (TEALS Discourse Account Required)