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

  1. Use parameters to generalize the purpose of a function.

    Code Studio: See rubric on bubble 15

Agenda

Warm Up

Activity

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

Warm Up

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

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?

  • Functions with Parameters
  • 2
  • (click tabs to see student view)
View on Code Studio

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
  • 4
  • 5
  • 6
  • (click tabs to see student view)
View on Code Studio

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.)
View on Code Studio

Student Instructions

View on Code Studio

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.
  • Iteration and Parameters
  • 7
  • 8
  • 9
  • (click tabs to see student view)
View on Code Studio

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?

View on Code Studio

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 calls moveBug(color) on every bug color.
  • (Hint: Look at the detectHits() function for clues how to do this.)
View on Code Studio

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 call checkNote(note) on everything in the "notes" array.
View on Code Studio

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?
View on Code Studio

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 a loopStar(color) function that will work with any star.
  • Test your function by calling loopStar("red") and loopStar("blue") inside the loopStars() function.
View on Code Studio

Student Instructions

Star Chaser

Now you'll need to make all the stars loop around the screen.

Do This

  • Use a for loop inside your loopStars() function to make every star in "starArray" loop around the screen.
View on Code Studio

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 a moveStar(color) function that can move any star.
  • Use code from the checkRedStar() function to make a checkStar(color) function that works for any star.
  • Test your code by calling checkStar("red") and checkStar("blue") inside your event block.
View on Code Studio

Student Instructions

Star Chaser

Now make it work for all the stars

Do This

  • Create a checkStars() functions that uses a for loop to check every star in the star array.
  • Test your code by calling the checkStars() function inside the event block.
View on Code Studio

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.
View on Code Studio

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.
View on Code Studio

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.