Functions are blocks of code that you put together to complete a specific job. Think of a function like a single block that you make, so you can use it over and over again. Just like other blocks, you can put it in your program anywhere that you need it, and as many times as you want.
With BlocklyProp, making a function is easy. Take a look at the example below:
To make a function, enclose a group of blocks with the define function block, and then give it a name. After that, in the FUNCTION menu you will see a new run function block with that name, to use in your program. In this example, when our BlocklyProp program gets to the run function “printAge” block, it jumps to the matching define function block and runs the code inside of it. Upon completion, it jumps back and the program continues on.
Functions are handy if you have to repeat the same code in a few different places in your program. Instead of using many blocks over and over, you can place those blocks in a function. Then just use one run function block wherever you need it in your program.
Blockly Variables are Global. Did you notice that the variable item countdown was defined right inside the run function "printAge" block? That did not prevent it from being used inside the oldAge function. In BlocklyProp, all variables are available for use by any other part of the program. This is not always the case in some programming languages, as you might learn if you study C.