3.1 Variables

So far, we have had to type all of our values into each of the commands we have issued. That is fine for simple activities, but for more complex projects this would get very tiring very quickly. It also makes it difficult to modify the drawings we create. The idea of storing values for subsequent use is very common in programming. Variables can be used to give a name to (and store) information for the life of our program.


3.2 - Creating Variables

You can think of a variable as a kind of box which can hold some data for long term storage. In order for this system to work, we first need to create a a variable (build the box, so to speak) before we can put anything into it. Doing such a thing in Blockly is straight forward: open the variables section of the toolbox and press the create variable... button. A dialog window will pop-up, asking you to give a name to this box.

If we created a variable called Square Size, we would see the following blocks appear in the toolbox under the Variables tab:

By default, the variable will be empty; There is nothing stored in the box. We can use these new blocks to give the variable a value, and retrieve that value later.


3.3 - Setting a Variable's Value

The block used to give a variable a value is shaped differently than the other blocks we have seen so far: . As you can see, it has a jigsaw cutout on the right-hand side of the block. This is an area where an additional block can be placed, similar to stacking the blocks vertically. Variables can hold many different types of values, but for now we will be sticking to numeric values.

Using the block (found under the Numbers tab in the toolbox), we can provide a value for a variable that has been previously created for Blockly. We just connect the two together, and type whatever value we wish to have stored in that variable.


3.4 - Using a Variable's Value

The block allows us to fetch the value of the variable named in the drop-down menu. Anywhere that you see that block, you can substitute the value you have previous set it in its place. You will notice that our other Turtle blocks have changed slightly.

These new blocks accept the blocks that specify our variables, allowing us to use the variables in our commands to the Turtles. This also means that if we want to just specify a value to our commands, we have to drop one of our new number blocks into the space as well.

Then, if you want to make the shape you draw larger, you simply need to change the value that is stored in the Square Size variable:


Execises

  1. Create a blockly program which draws a triangle roughly centered on the turtle drawing area. Your program MUST use a variable to store the length of each side AND a variable to store the amount to turn at each vertex. Make sure you try running your program with different values for the length of sides.

  2. Challenge
    Create a blockly program that can erase the staff lines, and move them to another x and y location stored in variables in your program. You should be able to change the variable x and/or y to create a new staff line in this new location. In the following example, the staff lines have been moved to x = -300, y = 200