Beginner Coding with Blocks
Chapter
>
Level

Functions and Procedures
Using Operators in Functions

Objective

Collect all the berries and check if you have enough to mash by using operators in custom functions.

There are several berries in the field that you could collect and mash to form a puree however you need to see if you have enough berries for a serving. To facilitate this, use a custom function to have it run a number calculation for you.

First thing we must do is set up a variable named serving, this is the amount needed for a portion of food, this variable should be the total amount of berries in the field (12) divided by three (3). Use the set variable block and the divide math block to calculate 12 / 3.

You must count the total amount of berries in the field yourself and enter the number. The reason it's divided by three (3) is because there are three (3) types of berries in the field: red berries, blue berries and black berries. The value of the serving variable is the average number of berries in the field (4).

Set up a custom function for future use named mash_berries using the define function block with a parameter called item. This function checks if you have enough berries of the variable you input in it and speaks a message. Use if blocks to check if item is greater than, equal to, or less than serving. Use text join blocks to combine messages with calculated values (like item - serving or serving - item).

Collect all the berries in the field, and store the number you collect of each type in the variables: red_berries (4), blue_berries (3) and black_berries (5) which are the purple berries present in the field. Head for their corresponding X marks and use the call function block to call mash_berries entering each variable as an argument.

Complete the level by doing this with all three berry types, the signs next to the X mark telling you which berry should be checked at that location. The top X mark being black berries and the bottom being blue berries.

Code book