Students will be able to...
- Define and identify:
- abstraction
def
(new Python keyword)
- Create functions
- Create a function contract0
- 3.02 Slide Deck
- Do Now
- Lab - Birthday Song & Random Cards (docx) (pdf)
- Associated Reading
- Read through the Do Now, lesson, and lab so that you are familiar with the requirements and can assist students.
- Video Resources:
Duration | Description |
---|---|
5 Minutes | Do Now |
10 Minutes | Lesson |
35 Minutes | Lab |
5 Minutes | Debrief |
- Students should take 5 minutes to follow the instructions on the Do Now in order to create/manipulate a user-defined function.
- 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.
- 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
- 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.
- 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).
- Check student progress and completion of the lab. Wrap up by taking any final questions.
If students are moving quickly, they could go back and use functions to improve an old project.