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.
定义 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.#
定义 greeting
[Define a My Block to display a message.]
打印 [Hello!] ▶
当开始 :: 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.#
定义 turns (number) :: custom
[Define a My Block with a parameter.]
重复 (number)
驱动 [向前 v] (50) [毫米 v] ▶
[右 v] 转 (90) 度 ▶
当开始 :: hat events
[Call a My Block start turning.]
turns [4]