Activity: Reducing Redundancy using Functions

Besides simplifying long sections of code, functions are also regularly used to reduce redundancy in code, similar to loops.

Using functions, we can take code that is repeated in multiple locations, and keep it in one centralized location. That way, when there are changes needed or bugs found, the code can be updated in a single place, instead of in several (or a hundred different) locations.

In this activity, students will:

  • reduce redundancy using functions

Example #1a: Movement

Link to Video)

  1. Review the code below
  2. Create the sample code and run the code
  3. Save the code for the example (name it “moveSprite”)
Playerspriteof kindmySprite2020mySprite"Hello!"1000mySprite10mySprite10Middle C1/2beatmySprite"Hi!"1000mySprite10mySprite10Middle C1/2beatmySprite"I'm here!"1000mySprite10mySprite10Middle C1/2beatmySprite"Bye!"2000mySpritedestroypausemssayplay toneatforchangeybychangexbypausemssayplay toneatforchangeybychangexbypausemssayplay toneatforchangeybychangexbypausemssaysetposition to xysetmySpritetoon start

This code may seem like it’s easy to simplify using loops at first, but there is a problem with that approach; each say block says something different!

In this case, it is easier to use functions to reduce redundancy; that way, we can capture the portions of the code that are repeated, without losing the details that are actually different.

In this case, the pause, movement, and tone all happen in the same order throughout the code, so putting those pieces into a function reduces that redundancy. If it is later decided that playing a tone of Middle E works better, then only one block needs to be changed in one location, instead of 3 different blocks.

Example #1b: Movement using Functions

  1. Review the code below
  2. Create the sample code and run the code
  3. Save the code for the example (name it “moveSpriteUsingFunctions”)
functionmove1000mySprite10mySprite10Middle C1/2beatplay toneatforchangeybychangexbypausemsPlayerspriteof kindmySprite2020mySprite"Hello!"callmovemySprite"Hi!"callmovemySprite"I'm here!"callmovemySprite"Bye!"2000mySpritedestroypausemssaysaysaysaysetposition to xysetmySpritetoon start

Student Task #1: Simplification

Link to Video

  1. Review the code below
  2. Create the sample code and run the code
  3. Create a function to replace the series of blocks that are repeated each time a projectile is made (hint: The function should include 4 blocks that appear in the same order 3 separate times)
  4. Reduce the redundancy in the code using your newly created function, without changing the behavior of the game
  5. Challenge: change the behavior of the game by making the projectiles move at twice the rate in the horizontal direction (from -20 to -40), and by making the play tone block play a Middle A
-200Playerprojectilevxvyof kindprojectile-30500Middle C1/2beat500-200Playerprojectilevxvyof kindprojectile-30500Middle C1/2beat500-200Playerprojectilevxvyof kindprojectile-30500Middle C1/2beat5002000LOSEgame overpausemspausemsplay toneatforpausemschangeybysetprojectiletopausemsplay toneatforpausemschangeybysetprojectiletopausemsplay toneatforpausemschangeybysetprojectiletoon start

Student Task #2: Events

Link to Video

  1. Review the code below
  2. Create the sample code and run the code
  3. Create a function called “buttonPress” and copy over the behavior from the on A button pressed event
  4. Replace the contents of both the on A button pressed and on B button pressed blocks with a single call function buttonPress
  5. Challenge: add in both a set projectile ax to and a set projectile ay to to function buttonPress, and set the newly created projectile to have random accelerations between -50 and 50
Middle G#1/16beat1-100100pick randomto-100100pick randomtoPlayerprojectilevxvyof kindprojectileONsetghostsetprojectiletochange score byplay toneatforonBbuttonpressedMiddle G#1/16beat1-100100pick randomto-100100pick randomtoPlayerprojectilevxvyof kindprojectileONsetghostsetprojectiletochange score byplay toneatforonAbuttonpressed"Press A and B!"015start countdown(s)set score tosplashon start

What did we learn?

  1. Does the use of a function to capture repeated code make it easier or harder to make changes to those repeated sections in the future?
  2. Are there any possible downsides to moving repeated code into a function?

Teacher Material