Table of Contents | Previous Page | Next Page
Copyright Don Sleeth, 1998 ©

Page 7 - Let's Talk Variables

Load in your work

From the file menu, open your project named Page6. Then from the file menu select "Save Project As..." and save your project as Page7

In the procedures page, your program should look like this:

	
====================
to Main
    cc
    talkto "t1
    setsh [Horse1 Horse2 Horse3]
    repeat 20 [Square]
end
====================

to Square
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
end

to Start
    Main
end
	
We will be entering lots of 'experimental' code in this page and will not be using it in Page 8. Be sure to save it as Page7 so that we don't mess up Page6.

Let's talk variables

When you start a program, such as MicroWorlds or Word or Excel, you usually double click on an icon. This is an instruction to the operating system to look on the hard drive and find the program that is associated with the icon. When the operating system finds it, the operating system reads it into memory (also called loading it) and executes or 'runs' the program. This is why in the PC world, files that run usually end with ".exe", exe for execute. Memory is the place where programs are stored and and where they live as they are running. This is the RAM that you buy when you buy your computer and there are millions of places, each with an address, where the program and we as programmers, can store things.

If we want our program to store a number, we can tell it to. But if we want to use the number later or change its value, we need a way to tell the program which storage place we are talking about. So we use names.

When we want to create a storage place to hold a value, we give it a name. Then it can hold something (it doesn't have to be a number, it can also be a word or a list). We use one built in procedure (local) to reserve and name a storage place, another built-in procedure (make) to put a value in it and another built-in procedure (thing) to see and use what is in the storage place. Let's set up a test program to investigate variables.

Test program

Delete the lines of instructions in Main. This is what you should now have:


====================
to Main

end
====================

to Square
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
end

to Start
    Main
end
	

Now we will make a little procedure to use to experiment with variables.

Write the following procedure:

		
to TestVar
    local "Var1
    show "Var1
end
	

and call it from Main by editing Main to be

		
to Main
    TestVar
end
	

As always, it helps to view the jigsaw puzzles:


Now go back to your program page 1 and click on the Start button. You should see:

The built-in procedure local is a procedure with one input and no outputs. It tells MicroWorlds to reserve a spot in memory to store things in and it tells MicroWorlds that we will refer to this spot as "Var1. The input to local is the name that we want the variable to have.

Show is a very handy procedure that takes one input, a word or a list and prints it on the Command Center. It is a handy procedure to use for experimenting and 'hacking around'.

The line, show "Var1, is an instruction to put the word Var1 on the Command Center. This is not really what I meant! I want to know what the value is inside the memory spot named Var1. Well, MicroWorlds Logo has another procedure which outputs the value in a named memory spot ( a variable). It is a procedure that has one input, the name of the variable and one output, the value that is stored there. It also has a funny name thing.

Change your TestVar procedure as follows:

		
to TestVar
    local "Var1
    show thing "Var1
end
	

and run your program. Here is the result I get:

We get an error message, telling us that we haven't put a value into the storage space! When we look at our procedure we can see this is true, we made a storage space (a variable) but we didn't tell MicroWorlds what to put it in it. It is a programming error to try to use a value from a variable if none has ever been stored there.

We have one more procedure to learn and then we can get into some serious variable hacking! There is a built-in procedure make that does the job of storing values in previously named variables. It takes two inputs and has no outputs. The first input is the name of the variable and the second is the value to be stored.

Change your procedure to the following:

		
to TestVar
    local "Var1
    make "Var1 20
    show thing "Var1
end
	

Lets take a look at what we have in our TestVar procedure this time.

  1. We use local to make a variable named Var1
  2. We use make to put the value 20 in the variable.
  3. we use show to show the output of thing, which we tell to look in Var1.

I think it should all work this time! Before you run your program, so that your Command Center shows only the newest messages, add the cc instruction in main, like this:

		
to Main
    cc
    TestVar
end
	

This of course just clears out the Command Center as you start your program each time. Now run your program.

Bingo! The procedure thing has as its output, the value we stored, which was 20! Let's put in 50 and see what happens.

		
to TestVar
    local "Var1
    make "Var1 20
    show thing "Var1
    make "Var1 50
    show thing "Var1
end
	

This experimenting has shown that you can replace one value in a variable with another, and the first value is gone forever. What about storing a word? Try this :

		
to TestVar
    local "Var1
    make "Var1 20
    show thing "Var1
    make "Var1 "Me
    show thing "Var1
end
	

Pretty neat, eh! Let's make another variable to play with

		
to TestVar
    local "Var1
    make "Var1 20
    local "Var2
    make "Var2 50
    show thing "Var1
    show thing "Var2
end
	

Here is an arithmetic procedure that is easy to use, sum. It has one output and takes 2 inputs, which must evaluate to numbers.

Try this

		
to TestVar
    local "Var1
    make "Var1 20
    local "Var2
    make "Var2 50
    show thing "Var1
    show thing "Var2
    show sum 20 15
    show sum thing "Var1 15
end
	

It is an excellent exercise to draw the jigsaw puzzle form of a line of instruction, especially before you run it. It can really help you learn what's going on. It is neat to see the way the logo procedures all fit together with their inputs and outputs. I especially like the way a procedure will wait until another procedure is evaluated in order to get its required input. If you don't have a firm understanding of this, don't worry, you are at the early parts of these lessons and your Logo learning. We will have more to say about this later. However you should really play with TestVar and sum to try to understand how all the inputs get satisfied.

Try adding these lines to TestVar

		
    show sum thing "Var1 thing "Var2
    show sum thing "Var1 sum 15 30
	

And of course, make up your own lines to experiment with.

Be like Logo

Another way to be sure you are understanding how a Logo instruction works is to "Talk" your way through an Instruction. I will do a couple:

			
    show sum 20 15
	
  • I am MicroWorlds Logo. I see the procedure show. This procedure tells me to take one input and put it on the Command Center. I need one input.
  • I see the procedure sum. It has an output that I can use to satisfy my need for an input. But first I need 2 inputs for sum before I can calculate the output.

  • I see the procedure 20. It has one output and no inputs to worry about, great, that is one input to sum satisfied.

  • I see the procedure 15. It has one output and no inputs to worry about, great, that is the second input to sum satisfied.

  • Now I know the output of sum, it is 35

  • So now I can satisfy the input to show. I will put 35 on the Command Center.

Whew! One more.

			
    show sum thing "Var1 sum 15 30
	

Assume "Var1 holds the value 10.

  • I am MicroWorlds Logo. I see the procedure show. This procedure tells me to take one input and put it on the Command Center. I need one input.
  • I see the procedure sum. It has an output that I can use to satisfy my need for an input. But first I need 2 inputs for sum before I can calculate the output.

  • I see the procedure thing, it has an output that I can use to satisfy one of my needs. However, first I must satisfy its need for an input.

  • I see "Var1. It is quoted so it is a name, thus it has no inputs and it will satisfy the input of thing. Now I can calculate the output of thing. I look in the memory spot named "Var1 and I see the value 10, so that is the output of thing. Good, that's one needed input down, one to go.

  • I see the procedure sum. It has an output that I can use to satisfy my need for an input. But first I need 2 inputs for sum before I can calculate the output.

  • I see the procedure 15. It has one output and no inputs to worry about, great, that is one input satisfied.

  • I see the procedure 30. It has one output and no inputs to worry about, great, that is the second input to sum satisfied.

  • Now I know the output of this last sum, it is 45 so that is my second needed input.

  • I now have the 2 inputs to the first sum, namely 10 and 45, so I can calculate the output of the first sum. It is 55.

  • So now I can satisfy the input to show. I will put 55 on the Command Center.

  • I quit!

Save your work

You can save your work as Page7, you may want to use it for further experimentation. On the next page we will not be using this project.

Table of Contents | Previous Page | Next Page