Functions Blocks

Overview

A function is a block of code that performs a specific task. If you use the same code set multiple times in your project, you can work more efficiently using functions. Clear function names also make your code more readable.

Video Tutorial

Create a Function

Use the following block to create a function and give it a name.

One example of a function is below. This function will create and shuffle a list of items.

Once you have created your function, you can use it at any time using the function block you have named.

Create a Function with an Output

You can also create a function that returns an output.

Conditional Outputs

Adding multiple "return" blocks to your function allows you to provide different outputs depending on specific conditions. This means your function can evaluate various scenarios and return distinct results based on your predefined conditions.

For example, in the block combination below, the function returns a description of the quality of the item being reviewed according to the ratings provided.

The function stops when the "return" block fires. For example, in the above example, if the rating is 100, the function will return Perfect and then stop. It will not run the other tests and won't return Excellent. Good, Fine, etc.

Inputs and Functions

Click the plus icon to add input variables to your function.

You can then treat that input as a local variable in your function.

In this example, the function "change font size" takes a number as an input and sets the screen's text to this font size. The number can be fed in as the number selected on a slider, or the user can click a reset button to set the font size back to 18.

Last updated