Introduction to the function

Function is a type of block where scripts are stored. It turns long stacks of blocks into shorter ones.

For example:

  • Below is a piece of code which can build a tree at the pointer's position by right-click.

  • Define this piece of code as function, and we can find a "build trees" block in the "Procedures".

  • After defining the function, this block will invoke the "build tree" function and run the stored script.

Difference between function and broadcast:

Broadcast blocks are run at the same time. For example, When "broadcast [ ]" block is run, the process is divided into two parts. One part is to run the script below "broadcast [ ]" block, and the other part is to run the script below "when I receive [ ]" block. However, if the function is invoked, only when the script in function has finished running, will the blocks below the function be run. For example, if you want to build trees with "repeat 10 times" block and broadcast blocks:

  • We will find only two trees are created.

  • The repeat block here only leaves a very short interval before sending out the next broadcast. After receiving the broadcast “Build trees”, the blocks begin to select a random spot for the drone's position, but at this moment, the repeat block has sent out the next broadcast. The blocks receive the new broadcast and start over to locate drone's position again.

  • If we use function blocks, the repeat block has to wait until the script inside the function block is finished. So, the program will create 10 trees for sure.

results matching ""

    No results matching ""