COMP203: Lecture 4


Syllabus | Homework and Assignments | Grading Rubric | Midterm Exam | Final Project

When circumstances allow, I'll be typing up bits of my lecture notes and posting them online. These may or may not bear any resemblance to the actual lectures.

Chapter 3

Chapter 3 teaches us to write procedures that require input. This is the first step toward writing procedures that do different things under different circumstances.

Inputs and Variables

The big new idea that we need in order to write these procedures is that of a variable.

A Logo variable is a storage space for information.

Variables in Logo are similar to variables in algebra in some ways. In other ways, variables in Logo are different from variables in algebra.

Think about the variable x in the expression 3x2 + 5x + 2. One of the most important things you can do with that variable x is replace it with a value. For example, when x = 1, 3x2 + 5x + 2 = 10. Using a variable here allows us to do the same calculation with a different value; when x = 0, 3x2 + 5x + 2 = 2.

Variables can be used much the same way in Logo. Observe how the variable named x is used in the following operation:

to evaluateat :x
  output 3*:x*:x + 5*:x + 2
end
Procedure evaluateat is an operation which is very similar to the function f(x) = 3x2 + 5x +2. (We discuss writing operations in more detail later.) Try running it:
? print evaluateat 1
10
? print evaluateat 0
2
The variable named x is similar to a variable in algebra in that it can have different values at different times. (The author doesn't like us to use x as a variable name; this should be the last time we do.)

One way that Logo variables are unlike variables in algebra is that the values stored in the variables may not make sense:

? print evaluateat "cookie
* doesn't like cookie as input  in evaluate
[output 3*:x*:x + 5*:x + 2]
Logo is perfectly happy to store the value of the word cookie in the variable named x. If Logo's multiplication operation had rules for multiplying words, we might not even have gotten an error message.

Think of variables in Logo is as storage space for values. Logo doesn't care what values you store in that space, but some of its procedures will care what comes out of storage -- putting your textbook in the refrigerator doesn't make it edible!

Make

There are (at least) two ways to store a value in a variable. The first is to use the value as the input to a procedure. In the example below, the input to the procedure greet is stored in variable name:
to greet :name
  print sentence "Hi word :name "!
end
Write your own procedure story that accepts a (single) name as input and outputs a simple tale about a person with that name.
? story "Heidi
Heidi played with the cat.
The other way to store a value in a variable is to use the command make to put the value directly into the variable. For example:
? make "value 7
? print :value
Notice that the inputs to make are the name of the variable (as indicated by the quotation mark) and the value to be stored in that variable. On the other hand, the input to print is the value stored in the variable. Use a colon (:) before the variable name to tell Logo to use the value stored in that variable. Some programmers don't like the make command because it allows us to change the value of a variable or define a new variable mid-procedure. While it is true that programming without it is quite elegant, make can be a very useful command.

Syntax and Variables

Computers aren't very smart, and have very specific expectations. We'll now review those expectations so that you won't surprise the computer too often by typing something it doesn't know how to handle.

Remember that procedure names have to be all one word. The reason for this is that when you're defining a procedure, anything after the procedure name describes the inputs to the procedure. So in procedure evaluateat, evaluateat was the name of the procedure and the :x after the space indicated that procedure evaluateat would require one input.

Not surprisingly, the :x also indicated that the name of the variable would be x. What is the : for? Page 43 explains that the : is a shorthand way of saying "the value stored in variable". So 5*:x means "five times the value stored in variable x". In the procedure greet,

word :name "!
tells Logo to look up the value stored in variable name and append a ? symbol to that value. As we remarked earlier, when we see:
make "number (:number + 1)
in procedure increment the " tells Logo that number is the name of the variable whose value is to be changed. There are quotes in front of number because we want to give procedure make the name of the variable, not its value (which is what we get if we replace the " with a :) or the result of running the procedure named number (which is what we get if we leave out the " entirely).

Remember that :number is the value stored in the variable named number, "number is the name of the variable named number, and number may or may not be the name of a Logo procedure. (And naming a variable number doesn't guarantee that the value stored in it will be a number!)