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.
In this tutorial:
Required knowledge:
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.
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
- Activate the My Blocks Code category
- Click the Make a block button
- Enter a name for your code block: drawSquare
- 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.
- An instance of your new code block will appear in the code window as well as in the My Blocks category
- Left click drag the code block using the first block, the
pen down
block - Attach it to the function you created
- Your new function is complete
- 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]
- Select the My Blocks category
- Right-click your custom drawSquare block and select Edit
- Click on Add an Input
- Edit the text prompt so that it simply reads number
- Click the OK button
- The drawSquare function (code block) now has a function parameter (takes an input amount)
- The drawSquare function (code block) now has a function parameter (takes an input amount)
- Left-click drag the number parameter from the drawSquare function
- Drop the number parameter in the parameter of the repeat code block
- 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.
- Scratch Function (no date) Programming. Available at: http://lam-programming.weebly.com/scratch-function.html (Accessed: 1 February 2024).
- MozDevNet (no date) Parameter – MDN Web Docs Glossary: Definitions of Web-related terms: MDN, MDN Web Docs. Available at: https://developer.mozilla.org/en-US/docs/Glossary/Parameter#parameters_versus_arguments (Accessed: 4 February 2024).