Scratch is a high-level visual programming language tool that interacts with users through diagrams and blocks that have the basics of a program inbuilt in it. Scratch is used to make interactive programs especially for kids using the block kind of interfaces so that they can easily learn languages and it is free to use. In this article, we will discuss the variables in scratch.
Variables in programming terminology are simply a container that can store some value. We can simply think of a box which has a number in it. The number inside the box can be increased or decreased using an increment or decrement operator as and when the program requires.
Variables basically store a varying value in the memory. They are designed in such a way that they can store only one value inside it and the value stored inside it may vary from numerics to strings or boolean. Being able to hold this bit of information allows us to reference and manipulate it at many different places in a program. This ability makes variables incredibly useful. For example, 123, abc, true/false, etc.
Scratch contains the following variables blocks:
- Variable: This block is used to reports the value of a variable
- set variable to 0: This block sets a selected variable to a specified value.
- change variable by 1: This block changes the value of the selected variable.
- show variable: This block display the specified variable in the project player.
- hide variable: The block reverses the “show variable” block.
Types of variables
In scratch, there are three types of variables:
- Global variable: It is the default variable. It means that it can be changed or accessed from any sprite in the project or stage, regardless of which sprite it was created on. Scratch allows the user or programmer to select a global variable by showing an option ” choose for all sprites”, or “choose and for this sprite only?”. If the user or programmer chooses “choose for all sprites”, then that variable becomes global as it can be accessed by anyone whoever needs it. All the global variables are stored in RAM and they are the default for those files in which they are created.
- Local variable: It is one that can only be changed or accessed from the sprite on which it was created. No other sprite can access this variable. Scratch allows the user or programmer to select a local variable by showing an option ” choose for all sprites”, or “choose and for this sprite only?”. If the user or programmer chooses “choose for this sprite only”, then that variable becomes local and only the current sprite has the access to it.
- Cloud variables: It is a variable that allows users to store variables on the server of the scratch. Cloud variables have the cloud-like symbol in front of the variable name and they update themselves very quickly. Starting from Scratch 3.0, the cloud variable supports only numeric data and the size of the data is only 256 characters. In a single Scratch project, you are allowed to create 10 cloud variables.
How to make a variable?
In Scratch, you can create variables in two different ways:
1. Using build-in variable: Scratch has an inbuilt variable named “my variable”, so the users can directly use that.
Here, you can also change the name of the variable. Also, if you want to display this variable on the stage, then check the checkbox present on the left side of the “my variable” block.
2. User-define variable: In case the user wishes to make his own variable, with a different name, then click on the “make a variable” button in the variable palette. After clicking the “make a variable block” a form will appear on the screen.
Now, fill in the name of the variable and the new variable is created as shown below.
After clicking ” OK”, the following image depicts that variable a has been created.
What is the use of variable in scratch?
Variables are of immense help to the programmers. Some of the uses of variables in Scratch are listed below:
- The most common use is to store values.e.g., if a project requires an user to input a name and then remember that name, the name is stored in a variable. The name can be later retrieved.
- It helps in writing efficient and less time-consuming scripts. As a variable’s value can change, variables are often used in blocks that contain a number.
Let’s understand the use of variables with two projects.
1. Without using variable
In this project, we move the script by 3 steps, increment the step subsequently, wait for 1 second, and repeat this 3 times.
Step 1: Click on the event block and choose 1st control block denoting start of program(drag it to center)
Step 2: Go to the motion block and drag “change x by 10” block and place it below the above step and change 10 to 3
Step 3: Go to the control block and drag “wait for 1 sec” and place it below the above block
Step 4: Go to the motion block and drag “change x by 10” block and place it below the above step and change 10 to 4
Step 5: Goto control block and drag “wait for 1 sec” and place it below the above block
Step 6: Go to the motion block and drag “change x by 10” block and place it below the above step and change 10 to 5
Step 7: Now run the program
Below is the implementation of the procedure:
2. With using variable
As we could see, the above code was cumbersome and time consuming. So, we can make this code simple and fast using variables. Let’s see how.
Step 1: Click on the event block and choose 1st control block denoting start of program(drag it to center).
Step 2: Go to the variable block and drag “set my variable to 0” below the above step.change 0 to 2.
Step 3: Go to the control block and drag “repeat 10” below the above step.change 10 to 3.
Step 4: Go to the motion block and drag “change x by 10” block and place it below the above step and change 10 to “my variable”.
Step 5: Go to the control block and drag “wait for 1 sec” and place it below the above block.
Step 6: Go to the variable block and drag “change my variable by 1” below the above step.
Step 7: Now, run the program.
Below is the implementation of the procedure:
We can see that the use of variables made the code efficient and contributed to a lesser line of code. The readability of code also increased when variables were used in the program.
How to rename the variable?
You can rename the variable the following the given steps:
Step 1: Right-click on the variable. You will get a list that contains two options “Rename variable” and “Delete the “my variable” variable”.
Step 2: Now select the “Rename variable” option. After clicking on this option, a dialogue appears on the screen.
Step 3: Now write the name of the variable and press “OK”.
How to delete the variable?
You can delete the variable the following the given steps:
Step 1: Right-click on the variable. You will get a list that contains two options “Rename variable” and “Delete the “newvar” variable”.
Step 2: Now select the “Delete the “newvar” variable” option. And your selected variable will be removed from the palette.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!