Revision as of 19:57, 17 February 2014 by Turkey3 (talk | contribs) (Added more)

Custom blocks, known as "procedures", "functions", or "methods" in some other languages, allow for one to make his or her own programming blocks. They allow a sequence of blocks to be grouped together and called by a single name. By utilizing custom blocks, large similar scripts with only minor differences can be broken down into one custom block. They help people follow the DOY rule of "don't repeat yourself".

The blocks in Scratch can be thought of as being procedures themselves, but made out of compiled code and not other blocks. Procedures can have inputs, or parameters, which can give the procedure more information. For example, the "move (10) steps" block has a single argument, the number 10, which tells the block how far to move.

Creating a Custom Block

To create a custom block, access the "More Blocks" category of the blocks palette, and select "New custom block". From there a menu will be opened prompting for the specific details of the block. The name can be typed into the text insertion located where the cursor is. The purple block resembles what the custom block will look like after it is created. When finished naming the block and adding any settings in the menu's Edit options, click "Ok". The custom block will appear in the palette and a hat block defining the block's procedure will appear in the scripts area. The block can later be modified by right-clicking on it in the blocks palette and selecting "Edit".

Procedures in Scratch

A "jump" custom block.
A "jump" custom block with number input.
Recursive factorial procedure — Scratch 2.0 alpha.

Procedures, or custom blocks, are one of the new features added in Scratch 2.0.[1]

Custom blocks are found in the "More Blocks" palette.[2] The palette starts off empty, and just shows a "make a block" button (similar to the "make a variable/list" buttons in the data palette).

Custom blocks can have arguments of strings, numbers, and booleans. After naming and adding arguments, a Define block is created. For each Define block (whether from the make a block button, or by dragging in from the backpack or another sprite), a custom block is created in the palette. The custom block is only available to the sprite that has the Define block.[3]

custom block [hi] (42) <touching color [#0bb]?>

define custom block (string) (number) <boolean>

Custom blocks can only be stack blocks, not reporters or booleans (though they may be added in the future).[4] They support recursion, so blocks can call themselves (This doesn't stop the current block executing, unlike broadcasts). This allows for things like creating fractals and finding the factorial of a number. They can be set to run atomically or "all at once".

Procedures in BYOB/Snap!

Snap!, previously called BYOB (Build Your Own Blocks), is a Scratch modification that allows procedures. Snap!'s procedures allow all 3 types of blocks, with arguments, and also recursion.

See Also


Cookies help us deliver our services. By using our services, you agree to our use of cookies.