Procedures (5/5)

1. Define a function

  • Function is a block that encapsulates script blocks, and shortens long blocks into a shorter one. If you want to shorten the scripts, you can use "define function" block.

  • We can name the fuction or change its name in the input box (names should not be repeated).

  • For example:

Name a function: DrawSquare, and then connect the blocks needed to draw a square.

In this way, we only need to use this defined function block to draw a square, and do not need to use so many blocks every time.

2. Use of function

  • A new block will be generated after a function is defined, which has the same name as the defined function.

As picture below, if we define a function as "function1", "function1" is the defined and shortened block.

  • Tip: Greyed out

When we use function blocks, we may delete "define function" block accidentally, which will make the "function" block invalid.

So, we have added "greyed out" effect to "function" block.

If you have deleted "define function" block, "function" block will be greyed out to help you to find the problem.

3. Parameter

  • Add and apply parameters. Using parameter can control the variables in functions.

  • When we add a parameter in "define function" block, some blocks will be changed.

For example:

Please note: "parameter" is not in the Block Boxes.

We can drag it out of "define function" block (duplicate), and connect to other blocks (except other "define function" blocks).

  • For example:

Add a parameter called "sidelength" to the function we just defined, and set it as the steps each time the square moves.

We can see that the block "DrawSquare" has changed, and there is one more place where data can be entered.

This parameter data is the side length of the square that we just set. Set it to 100, and it can draw the a square with sides that are 100 in length.

4. Returned value

  • If the function has returned value, this value can be exported and called.

  • By default, the returned value has a parameter. If you click "-" of this block, it becomes a block without returned parameter, which is used to exit the function.

  • Tip:

When we use "return" block with judgment blocks ("if" and "if-else" blocks), We must take full consideration of what may happen.

For example: return the Y position of the sprite when it is greater than 0, and show it on stage.

If the Y position if the sprite > 0, return its Y position.

Click this block set and the system displays a prompt: Error. Not all the branches return value.

It means that: we have already set that if the Y position of the sprite > 0, return its Y position, but we haven't tell the system what to do if the Y position of the sprite < 0. Then the system would report the error.

So, the conditions should be: If the Y coordinates > 0, return the value of Y position; otherwise, return 0.

Blocks can be:


Both methods are available. Now the system knows that returns the value of Y position of the sprite if it > 0; otherwise, return 0.

5. Data of function

  • When we connect "return" block with "define function" block, a new function block, the data of this function, will appear in Procedures.

  • Function data block is also displayed with the name of the defined function, and its name will change as the function name is changed.

  • When adding a parameter to a defined function, a input box will appear in the function data block.