Want to create or adapt books like this? Learn more about how Pressbooks supports open publishing practices.
4 Variables
Learning Outcomes
Create and name a variable
Set a variable to a value
Use the variable’s value in your program
Set the variable to a different value in the middle of the program
Why are variables useful?
Before we get into variables, you need to understand a situation where they would be useful. Let’s review how to write a program for a square.
Forward, turn left, forward, turn left…
Now make the square bigger by increasing the duration of all of the forwards for 2 seconds.
Now make it even bigger by increasing the duration of all of the forward commands to 3 seconds!
Phew! That’s a lot of work to change each line of code every time. How do you think we can make this code more efficient?
What is a variable?
Just like humans keep track of values in our brains, computers keep track of values in their program memory using variables. A variable is a placeholder that is assigned a value. Think of variables as storage containers in your program’s memory. You can create more than 1 variable, and they can hold various different values.
Creating a variable
You are going to create a placeholder for the driving duration. Then, you only have to change one line of code to apply the changes throughout our whole code. Open the “Variables” menu in Blockly.
Remember our analogy that variables are like program storage? Variables should have descriptive names so you can easily find that information later. Name this variable “seconds”.
Once a variable has been created and assigned a value, you can use it later on in your program. Whenever your program sees the variable name, it will replace it with its value that you assigned. Think of the program as looking into the program storage, finding the correctly labeled package, and checking its value inside.
Setting a variable value
Once you create a variable, new blocks will appear in the menu. One sets the value of the variable and one gets the value in the variable from your program storage.
Before you can use the variable in your program, let’s set the value to equal 1. First, move the set variable block to the workspace.
Open the “Math” menu and select the first block in the list with the number “0”.
Connect the number block to set the “seconds” block to 1. You can change the value in the number block using your keyboard.
Using variables
Once a variable has been created, you can use it in your program. Replace all of the blocks with the seconds value with the variable. From the “Variables” menu, choose the “get” blocks to retrieve the variable from memory. Think about “getting” the variable from storage!
Changing a variable
Now let’s make the square bigger. Instead of changing all of the individual forward blocks, now you only have to change one variable! Set the seconds variable to 2 seconds then run the code. What happens?
Practice changing the seconds variable. You can use decimals, too!
Creating more variables
Remember we said you can make more than one variable in a program? Now let’s create a variable so we can simultaneously change the speed value for all of the forward commands. Since the parameter is also called speed, the variable must have a different name.
Below the speed variable declaration, create a new variable power and assign it the value 40.
Now you have created a second variable and assigned it to the value 50. Next, you will need to use that variable in your program to set the speed of the forward command.
In each of the forward commands, set the speed to 40 using the variable power.
Great job! Now you can modify both the speed and duration variables.
Changing variables
A variable is named after the fact that it can vary, or change. You were changing your variable before running the program, but you can assign the variable a new value in the middle of your program, too.
For example, let the first three sides of the square have a duration of 2. But then change the variable to be 1 so that the last side of the square is smaller. Continue changing the variable to make a square spiral! It will look something like this:
To change the value of a variable in the middle of the program, use the “set” variable block. Take a look at what some example code would look like:
Review
Great job learning all about variables! From now on, you will be using variables in most future lessons. Some variables will store numbers, but others can store words or pictures!
In this lesson, you learned how to:
Create and name a variable
Set a variable to a value
Use the variable’s value in your program
Set the variable to a different value in the middle of the program
Test Your Knowledge
Review The Key Concepts
Solve The Problem
Scenario: You have 4 bus routes that your autonomous vehicle needs to travel. These bus routes all have different names (North Route, South Route, East Route, West Route).
Problem: How can you store, display, and call the different bus routes on Zumi?
Sample Solution
There are many different ways that we can solve the problem listed above. Please watch the following video to review a sample solution completed by Ontario Tech University Engineering Students.
Demo
Code
Common Errors
Errors can also occur while creating a solution to the problem. Please review the video below to view some of the common errors that can occur.