Beginner Coding with Blocks
Chapter
>
Level

Advanced Functions and Patterns
Creating Functions

Functions are reusable blocks of code that can return values. You can create a function once and call it multiple times, which makes your code more organized and easier to maintain. Functions that return values use the define function to return block.

For this level, create a function that returns "Grain", grab grains from the field, and use the call function block inside the build block to create grain stacks in the field. This teaches you how to organize code using functions.

Guide

Objective

Grab all grains in the field and compile them into hay stacks using functions.

The grain in the field needs to be put together and compiled into stacks. In order to do this efficiently, you can create a function that returns the build string. Functions are reusable blocks of code that can return values, making your code more organized and easier to maintain.

Use the define function block to create a function named get_grain_stack. This function should return the text "Grain" using a text block. Functions that return values use the define function to return block.

Once you've created the function, you can call it using the call function block. This will execute the function and give you back the value it returns. You can use this returned value in the build block.

Create a function that returns "Grain", collect all twelve (12) pieces of grain in the field (4 for each stack). Walk to the X marks, use the build block and connect the call function block to it. This way you can reuse the same function multiple times instead of typing the same text over and over.

Code book