Lesson 14: Functions with Parameters
App Lab | Maker Toolkit
Overview
The lesson starts with a quick review of parameters, in the context of App Lab blocks that they students have seen recently. Students then look at examples of parameters within user-created functions in App Lab and create and call functions with parameters for themselves, using them to control multiple elements on a screen. Afterwards, students use for loops to iterate over an array, passing each element into a function. Last, students use what they have learned to create a star catching game.
Purpose
In previous lessons, students have used functions to define blocks of code that can be used in multiple places in a program. In this lesson, students learn how to use parameters to generalize the purpose of a function. Parameters allow a program to specify the details of how a function works when it is called, rather than when the program is defined. Although students have seen functions with parameters earlier in the unit, this is the first time that they are expected to define and call their own. Students also learn how to use a for loop to iteratively pass in the elements of an array as parameters to a function, allowing them to use the same function on multiple elements on the screen.
Assessment Opportunities
-
Use parameters to generalize the purpose of a function.
Code Studio: See rubric on bubble 15
Agenda
Lesson Modifications
Warm Up
Activity (50 min)
Wrap Up
View on Code Studio
Objectives
Students will be able to:
- Use parameters to generalize the purpose of a function.
Vocabulary
- Parameter - An extra piece of information passed to a function to customize it for a specific need
Introduced Code
Teaching Guide
Lesson Modifications
Attention, teachers! If you are teaching virtually or in a socially-distanced classroom, we have recommendations for alternate lessons that can be used for this unit on physical computing. Click Here to read our recommendations for Unit 6.
Warm Up
Maker App Updates
For Windows & Mac users: we've recently updated the way that students can log in with Google using the Maker App. Please see this forum post for more information about these updates and how to continue using Google to log in to the Maker App.
Discussion Goal
Goal: Students should eventually see that parameters give a program flexibility. Sometimes, multiple commands are similar enough that it makes sense to combine them into one, but with a parameter to distinguish between their differences. Sometimes, such as the case of buzzer.frequency
, there are too make options to make a separate block for each one. Parameters allow a programmer to use a single solution to solve multiple, related problems.
Prompt: When you needed to access the pitch and roll of the accelerometer in the last lesson, you used the block accelerometer.getOrientation
, and you chose whether to get the pitch or the roll. Why do you think the creators make the program work that way, rather than having two blocks, one for the pitch and one for the roll?
What other blocks that you have seen take parameters? Why are parameters so useful?
Remarks
So far, the functions that you have used always did the exact same thing. Today, we're going to look at a way to make functions even more useful by giving them parameters, just like some of the blocks that you just talked about.
Activity (50 min)
Send students to Code Studio
Wrap Up
Prompt: Think back to some of the programs that you have made before. What are two times that you could have used functions with parameters? What would the parameter be? How should the function's behavior change when the parameter changes?
- Lesson Overview
- Student Overview
- Functions with Parameters
- 2
Student Instructions
Bug Crawl
Look at the program below, paying special attention to the function crawl(bug)
. What happens if you press the left or right button?
- Functions with Parameters
- Teacher Overview
- Student Overview
Discussion Goals
Students will have a chance to practice defining functions with parameters in the lesson, so it's not necessary for them to understand the exact process. Students should understand that parameters are useful because they let programmers use the same function to solve problems that are very similar, but with slight differences. If multiple functions are almost alike, but have a small difference, students may want to combine those functions into one, and use a parameter to account for the difference.
Questions to Consider
- How do parameters make functions more useful?
- How do you know when you can combine multiple functions into one?
Student Instructions
Clouds
This program makes the clouds move when you blow on the sound sensor, but it's not finished.
Do This
- Create a new function
moveCloud(name)
that will move any cloud. - (Hint: you will use almost all the same code as the
moveCloud0
function) - Call your new
moveCloud(name)
function inside the event block, once for each cloud name. - (Hint: this is similar to how the
loopCloud(name)
function is called many times.)
Student Instructions
Student Instructions
Color Picker
In this program, pressing the buttons at the bottom of the screen changes the color of the images and the color LEDs.
Do This
- Create a function
chooseColor(color)
that changes each of the images and the color LEDs to the given color. - Call your function in the event blocks so that all the buttons work.
Student Instructions
Fish Bubbles
This program uses the sound sensor in the same way as the clouds program. How many bubbles will move when you blow on the board?
Student Instructions
Bug Shaker
This program shakes bugs around when the board is shaken, but it's only working for one bug.
Do This
- Change the
moveBugs()
function so that it callsmoveBug(color)
on every bug color. - (Hint: Look at the
detectHits()
function for clues how to do this.)
Student Instructions
Music Player
This program lets you play music by waving a wand over the different notes, but it's missing a function definition.
Do This
- Create a
checkNotes()
function that will callcheckNote(note)
on everything in the "notes" array.
Student Instructions
Star Chaser
In this game, the user tries to catch the stars according to the color of the LEDs.
Do This
- Play the game, and discuss the following with a partner:
- What functions might this program need?
- What parameters should these functions have?
Student Instructions
Make your loopStar(color)
function
Right now only the red star works, so you'll need to change this code.
Do This
- Use the code from the
loopRedStar()
function to create aloopStar(color)
function that will work with any star. - Test your function by calling
loopStar("red")
andloopStar("blue")
inside theloopStars()
function.
Student Instructions
Star Chaser
Now you'll need to make all the stars loop around the screen.
Do This
- Use a
for
loop inside yourloopStars()
function to make every star in "starArray" loop around the screen.
Student Instructions
Star Chaser
All the stars loop, but you can still only catch the red star.
Do This
- Use code from the
moveRedStar()
function to make amoveStar(color)
function that can move any star. - Use code from the
checkRedStar()
function to make acheckStar(color)
function that works for any star. - Test your code by calling
checkStar("red")
andcheckStar("blue")
inside your event block.
Student Instructions
Star Chaser
Now make it work for all the stars
Do This
- Create a
checkStars()
functions that uses afor
loop to check every star in the star array. - Test your code by calling the
checkStars()
function inside the event block.
Assessment Opportunities
Extensive Evidence
The program works as described and includes all functions with parameters as described in the instructions. (moveStar, loopStar, and checkStar)
Convincing Evidence
The program includes multiple functions with parameters and generally works as described, but may include minor errors that affect gameplay.
Limited Evidence
The program includes at least one function with parameters, but there may be major errors in the code that prevent the game from working.
No Evidence
The program does not include any functions with parameters.
Student Instructions
Star Chaser
Now you can add extra stars, and your functions will work with them, too.
Do This
- In design mode, copy one of the stars, and change its name and color to "yellow".
- Add a new element, "yellow", to your star array.
- Test the game with your new star.
Student Instructions
Star Chaser
Now that you have a working game, you can challenge yourself with some different features.
Do This
- Start the user with 5 "lives", and take one away every time the user touches the wrong star.
- Make an end screen, and change to that screen when the user runs out of lives.
Student Instructions
Star Chaser
Now that you have a working game, you can challenge yourself with some different features.
Do This
- Make the player go back to the middle of the screen when the left button is pressed.