Functions Blocks
Last updated
Last updated
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.
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.
You can also create a function that returns an output.
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.
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.