The ability to create functions within a program allows you to write code once and reuse it. Scratch allows you to create your own custom code blocks, ultimately creating a custom function. This tutorial starts off where the Drawing a square with Scratch tutorial ended.

1. Starting point

We are going to use the code (see left) from the tutorial Drawing a square with Scratch as our starting point for this tutorial.

Separating code blocks in Scratch.
Separating code blocks.

Simply left click drag the pen down block (and the blocks attached below it) away from the when ... key pressed Events block.

2. Create a new block

  1. Activate the My Blocks Code category
  2. Click the Make a block button
  1. Enter a name for your code block: drawSquare
  2. Click the OK button

3. Use your new block

Now that you have created your function, drawSquare, you can call it like you would any other code block.

Calling your new function in Scratch.
  1. An instance of your new code block will appear in the code window as well as in the My Blocks category
  2. Left click drag the code block using the first block, the pen down block
  3. Attach it to the function you created
  1. Your new function is complete
  2. drag an instance of your function code block and attach it to your Events code block

Hit the Space bar and give your function a spin!

4. Edit your block

We are now going to change our function so that when we use it we can specify how long the sides must be. We do this by adding a parameter to the function. Then, when we call the function, we add an argument. The argument value is then passed to the function and used as a variable in place of the function’s parameter.

You will often see the terms parameter and argument used interchangeably. They are not however the same, the differences are:

  • Function parameters are the names listed in the function’s definition.
  • Function arguments are the real values passed to the function.
  • Parameters are initialized to the values of the arguments supplied. [2]
Editing a custom code block in Scratch.
Editing a custom code block in Scratch.
  1. Select the My Blocks category
  2. Right-click your custom drawSquare block and select Edit
Editing a custom code block in Scratch.
Editing a custom code block in Scratch.
  1. Click on Add an Input
  2. Edit the text prompt so that it simply reads number
  3. Click the OK button
Left-click dragging the function parameter as a variable in the move code block.
Left-click dragging the function parameter as a variable in the move code block.
  1. The drawSquare function (code block) now has a function parameter (takes an input amount)
Edited custom code block in Scratch.
  1. The drawSquare function (code block) now has a function parameter (takes an input amount)
  1. Left-click drag the number parameter from the drawSquare function
  1. Drop the number parameter in the parameter of the repeat code block
  2. Enter a number for the number argument when the drawSquare function is called

Hit the Space bar and give your function a spin!

5. Next steps

Now that we have created our own function, it is time to take advantage of it by using it with more than one sprite. See the tutorial Using your custom block in Scratch.


  1. Scratch Function (no date) Programming. Available at: http://lam-programming.weebly.com/scratch-function.html (Accessed: 1 February 2024).
  2. MozDevNet (no date) Parameter – MDN Web Docs Glossary: Definitions of Web-related terms: MDNMDN Web Docs. Available at: https://developer.mozilla.org/en-US/docs/Glossary/Parameter#parameters_versus_arguments (Accessed: 4 February 2024).

By MisterFoxOnline

Mister Fox AKA @MisterFoxOnline is an ICT, IT and CAT Teacher. He has a passion for technology and loves to find solutions to problems using the skills he has learned in the course of his IT career.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.