My Blocks#
Introduction#
My Blocks is the equivalent to functions in Python. My Blocks group code into reusable, efficient sections of code designed to perform a specific task. My Blocks can be called multiple times within a program, making code organization easier, and helping to avoid repeated code.
define myBlock
myBlock
Parameters |
Description |
---|---|
block name |
A name you give to your My Block. |
parameters |
Optional. Variables that accept input values when the function is called, allowing data to be passed into the function. It can be number or Boolean. |
Defining and Calling My Blocks#
My Blocks#
If a My Block does not require input, you can define it without parameters.
When started, calls a custom block named ‘greeting’ to print ‘Hello!’ on the screen.#
define greeting
[Define a My Block to display a message.]
print [Hello!] on [Brain v] ▶
when started :: hat events
[Call a My Block to display the message.]
greeting
My Blocks with Parameters#
You can also add parameters to My Block, which let you pass in information the My Block needs to work.
When started, calls a custom block ‘named_greeting’ with a parameter. It prints ‘Hello, ’ followed by the given name on the screen.#
define turns (number) :: custom
[Define a My Block with a parameter.]
drive [forward v] for (50) [mm v] ▶
turn [right v] for (90) degrees ▶
when started :: hat events
[Call a My Block start turning.]
turns [4]