How to say variables
Help Contents
First Steps In Logo
How to say variables
How to say variables
If Logo is your first programming language it is possible that you have not been exposed to variables before.
What are variable and why programming languages need them?
Imagine you are responsible for writing fire escape instructions. When you write it you can refer to different people as "the one who is on duty".
You write this just because you don't know who will be on duty at actual time of the fire.
Of course, you can put names in the instructions, but then you must provide a separate instruction set for each person who could be on duty.
In Logo you can use words as names of values.
The important thing is that you can change the value and use them by the same name.
That's why these names and their associated values are called variables - you can vary the value of a variable, but keep your program unchanged.
This is one of the most important features of computer programs - you write a program once and than run it with different set of data.
A variable in Elica Logo is defined by its name, value and position.
You already know that variables' names are words and their values can be anything - words, numbers, lists and actions.
When you want to say the name of a variable, ignoring its value, follow the rules for words and put " before it.
This is often need to create a variable. If you want this you can just ask Logo in a very natural way to make it.
make "a 5
make "b [a list of five elements]
The first instruction creates variable called a which has a value 5.
The second one creates b and assigns it a list. Make is one of the few actions about which Logo has prebuilt knowledge how to handle. Later on you will find a longer description of the action.
What about values? How to refer to a value of a variable?
There is a special action in Logo which you can use for this purpose.
If you use the action
:
before a word Logo will consider the value of a variable with a name the same as the given word.
Let you want to ask Logo to find the sum of two numbers stored in variables a and b.
To do this you need to ask something like
:a+:b.
Logo is a powerful language. There are many explanations, but one of the coolest is that Logo can work with variable which names are not known in advance.
Consider the next example:
make :a "b
How will Logo interpret it?
It will first find the associated value of a variable called a.
This value (rather than a) will be used as a name of a variable to create.
Except names and values, variables have positions. The position is a description of where a given variable exists.
What are the possible places for a variable?
In Logo each variable can be placed inside another variable. In other words, each variable can contain only a value, but another variable in addition.
Even more - a variable can contain many variables. Some people call these variables children. Other people prefer the word local variables. There is a third group - the hard programmers - that would call them fields, members or methods.
As it always happens it doesn't matter how you call them - Logo does not use these words. They are used by humans and when you talk with your friends or classmates about Logo, it will be better to use the same words.
So, let's go back to positions.
If you want to define a variable a which is inside b, then you can use the name b.a .
This instructs Logo when lokking for the value of a, to locate this variable inside variable b.
Of course you can nest variables deeper. For example, d.c.b.a is the name of variable a, which is a child of b, that is nested in c, which is a local variable of d.
make "a.x 10
make "a.y 20
make "a :b
print :b.x :b.y ; 10 20
Examine the example above carefully. It shows one basic feature of local variables.
When Logo takes the value of a variable it takes not only the value but all children too.
The first two lines define a variable a which has two local variables - x and y.
When Logo creates variable b it copies a's x and y to b.
So from now on there will be two variables called x (one in a and one in b) and two called y.