Beginner Coding with Blocks
Chapter
>
Level

Advanced Functions and Patterns
Functions with Parameters

Objective

Grab materials off the field and use them to create roads.

The roads leaning to the old shed are bare, grab the materials in the field and build up the roads. There are two types of roads, those made of "wood" and those made of "stone", you can use a function with parameters to create the build strings for different types of roads. Each road requires 4 items of its respective type in order to build.

Use the define function to return block from the Functions category in the toolbox to create a function named get_road. To add a parameter called material to the function, click the gear icon (mutator) on the function block, then add a parameter block in the mutator dialog and name it material. Inside the function, return the material parameter directly using a variables_get block. This way, the function can return different build strings depending on what material you pass to it.

Functions with parameters allow you to pass values into the function when you call it. You can call the same function with different arguments to get different results. For example, calling get_road with "wood" will return "wood", and calling it with "stone" will return "stone".

Grab all the materials in the field, and create a function named get_road with a material parameter. Walk to the X marks and use the build block with the call function block, passing "wood" or "stone" as the argument to build the different types of roads.

Code book