-
Take a look at the new dance you created. It probably takes several blocks to get Dash to do everything you want. Wouldn’t it be nice if you could tell her how to dance with just one block? You can do that by defining a function, which bundles up a set of instructions to complete a single task. Let’s build a dance function! Select the Functions category in the toolbox (you will have to scroll to the bottom).
-
Select the block that appears in this category. It is called Create/Edit Functions.
-
A new screen will open. This is the functions menu. Select Add Function in the upper right corner of the screen.
-
A pop-up will appear that asks you to type in your function’s name. You can call it “dance” or another creative name that you choose! When you’re done naming your function, press the Done button in the center of the screen.
-
A new row of buttons will appear near the top of your screen. Select the button that has the same name as your function.
-
A new screen will appear. This is called the function workspace and it works almost the same way as the main workspace. The one different thing about the function workspace is that it already has two blocks in it. Check out the Function Start and Function End blocks in the workspace. You will have to place all your dance blocks between these two blocks so that Dash knows that they are part of the function.
-
Select the first block for Dash’s dance in the center of the circle. Don’t include Drive Forward or Drive Backward (they’re not part of her dance). Hint: You can select blocks from the toolbox on the left, just like in the main workspace.
-
Place the block in the workspace between the two Function blocks (Hint: You can do this by selecting the Function End block.)
-
Repeat until you have all of Dash’s dance blocks inside the function.
-
You can run your function using the Play button in the lower right corner of the screen to make sure it works correctly.
-
When you’re happy with your function, select the Main Workspace button at the top of the screen in the center. This will take you back to the main workspace.
-
We’re almost ready to use your function! First we have to delete the old dance blocks between Drive Forward and Drive Backward in the workspace. To delete a block, first select it in the workspace.
-
A Trash button will appear in the lower right corner of the screen, where the Play button used to be. Select this button.
-
Now your block is deleted! Do this for all the other blocks between Drive Forward and Drive Backward.
-
Now you’re ready to put in your function! Select the Functions category in the toolbox.
-
Below the Create/Edit Functions block is a block with your function’s name on it! Select this block.
-
Place your function in the workspace where Dash’s dance should go (Hint: Put it between the Drive blocks).
-
Select Play. Dash can now read all the instructions for her dance from your one Function block!
-
When you finish, give yourself (and Dash), a high five and head over to
the Hour of Code Website to access your completion certificate!