In this lesson, students will learn about variables in coding. They will learn how to create and store values in variables, as well as how to use them in a program. |
Students will be able to:
|
1. The Need to Store Values When we write programs often we need to store values for later use in the program. Or we might want to store a value that is used multiple times throughout a program. For example, we might want to have the robot trace a square using four driveDistance blocks. Each block will have the same value for the distance to drive (the side length of the square). After we have the code working, we can change the distance values in each of the four blocks to have the robot trace a different square. But it would be even better if we could define a "side length" value for the driveDistance blocks to use and then just change that value one time, rather than having to change the value for each of the four driveDistance blocks. We can define and store such a "side length" value by using the concept of a variable.
2. We Store Values Using Variables A variable is like a container or a storage box with a label on it. The contents of the storage box represent the value of the variable, and the label represents the name of the variable. For example, we could have a variable named sidelength that had the number 8 stored in it as its value.
3. Create or "Declare" a Variable We create a variable (also called defining or declaring a variable) using the set variable block, found in the Variables block menu:
Clicking on the pop-up menu in the block allows us to give a name to the variable by choosing "New variable":
4. Give the Variable a Descriptive Name It is good programming practice to give descriptive names to variables, such as distance or height or sidelength, so the name indicates what is being stored. Variable names can have numbers in them, such as distance2, but should not start with a number (don't do something like 2distance). You also should not use spaces in variable names. If you want the variable name to have two words, then do something like sidelength or side_length or sideLength.
5. Give ("Assign") the Variable a Value We attach a blue number block (found in the Math menu) to the set variable block to give the variable a value. This is also known as assigning a value to the variable. For example, we can create a variable named "distance" and assign it the value 8 like this:
6. Code Blocks for Variables Once a variable has been created and assigned a value, RoboBlocky creates a code block for that variable in the Variables menu:
NOTE: If the code block for the new variable does not appear in the Variables block menu, simply click the "Variables" menu button twice so that the menu display updates. (The first time you click the menu button it hides the menu, so you will need to click twice.) The variable code block (distance, in this case) can then be dragged and inserted into other code blocks. For example:
This driveDistance block will drive the robot according to the value contained in the distance variable.
7. The Value of a Variable Can Change Throughout a Program Variables are called "variables" because their values can vary throughout the program. For example, we could change the value of the distance variable from 8 to 5 simply by using the set distance block again later on in the program. Example 1 shows how it works.
8. Variables and Data Take Up Space in the Computer's Memory When we create a variable and store a value in it, behind the scenes the computer reserves a section in its memory to put the value. It also gives it an internal label so that it knows where to find it or change it when needed. Computers store many different kinds of information in addition to numbers, such as letters and images. We often call such information "data." Different types of data will require different amounts of storage space in the computer's memory. To take a simple example, a very large counting number may require more space than a small counting number, because there are more digits to store. And a small image may require more space than several pages of text, because the image is made up of thousands of tiny colored dots that each need to be stored.
|
|
The example creates a variable named "distance" and sets it to a value of 8. Then it uses it to drive the robot forward 8 units. The code then turns the robot and assigns the distance variable a new value of 5, using it to drive the robot 5 units forward. |
The example creates a variable named "sidelength" and sets it to a value of 7. Then it uses the variable in four driveDistance blocks to have the robot trace a square. (Change the value of the sidelength variable and run the code again to have the robot draw a different square.) |
Grid Size: S M L | Simple View: | MinView: |
Coord: x-axis y-axis | Grid Lines: x-axis y-axis | |||
Grid: | 12x12 inches | 24x24 inches | 36x36 inches | |
72x72 inches | 96x96 inches | 192x192 inches | ||
1x1 inches | 2x2 inches | Fraction: | ||
xy-Range: | ||||
|
||||
|
||||
Quad: | 1 Quadrant | 4 Quadrants | 1&4 Quadrants | |
Units: | US Customary | Metric | Ruler: |
Labels: |
|
|
|
Font
px
|
|
Tics Lines: |
|
Width
px
|
Hash Lines: |
|
Width
px
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
|
|||||||||||
|
|||||||||||