COMP203: Lecture 7

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.

Vocabulary Quiz Wednesday

On clearscreen, back, forward, penup, pendown, sentence, word, first, butfirst, last, butlast, item, readlist, readword. Spelling counts!

Due Dates Changed

Note assignment due Monday of Spring Break.

Scope of Variables

Questions on the reading: If we think of variables as lockers in which data is stored, these questions come down to "when am I allowed to look at and change the stuff in your storage space?"

Programming languages have as many options for answering this question as we do in real life. Logo's solution is a simple one, though not the simplest.

In the example below (taken from page 50), top is the superprocedure, or boss, and bottom is the subprocedure.

to top :outer :inner
print [I'm in top.]
print sentence [:outer is] :outer
print sentence [:inner is] :inner
bottom "x
print [I'm in top again.]
print sentence [:outer is] :outer
print sentence [:inner is] :inner
end

to bottom :inner
print [I'm in bottom.]
print sentence [:outer is] :outer
print sentence [:inner is] :inner
end
This example illustrates the fact that a subprocedure can see the values of its superprocedure's variables (lockers). It also shows what happens when a subprocedure uses a variable with the same name as one in its superprocedure.

Can a subprocedure change the valuess of its superprocedure's variables?

Can a superprocedure see the values of its subprocedures' variables?

Can a procedure see the values of variables in procedures that aren't related to it?

If we ignore things like global variables and variables with the same name, Logo's rules for sharing variables look something like:

Global Variables

One exception to the rules above are global variables. These are like public lockers -- any procedure can see and change the values of a global variable. If you imagine what is likely to happen to the contents of a locker that's open to the public, you'll understand why programmers like to avoid using global variables.

The most sensible use of global variables is to store values that everybody needs and nobody wants to change. For example, you might store the value of e or pi in a global variable.

In Logo, to avoid accidentally creating global variables we have to specify which procedure a variable belongs to. If you're using functional programming (all variables are procedure inputs) this isn't a problem. However, if you use make to set or change variable values, you should use Logo's local command to prevent creation of extra global variables. Here are some examples to play with:

to makeglobal
 make "score1 0
 print sentence [The value stored in score1 is] :score1
end

to makelocal
 local "score2
 make "score2 3
 print sentence [The value stored in score2 is] :score2
end
Run these procedures, then see what happens when you ask Logo to:
print sentence [The value stored in score1 is] :score1
print sentence [The value stored in score2 is] :score2

Done with scope of variables

For more practice, work on writing a three question quiz which keeps track of your score on the quiz.

Writing Operations

So far all the procedures we have written have been commands, not operations. What do we need to be able to do with Logo in order to write an operation?

Operations provide information to other procedures by outputting data. The logo command output can only be used in a procedure. It requires one input value, and when it's evaluated it ends the procedure it's used in and causes that procedure to output that input value.

For example:

to hyphenate :name1 :name2
  output (word :name1 "- :name2)
end
or:
to tan :angle
  output quotient sin :angle cos :angle
end


to hypotenuse :leg1 :leg2
 output sqrt (:leg1 * :leg1 + :leg2 * :leg2)
end
Write a Logo procedure cube which accepts a number as input and outputs the cube of that number.
? print cube 2
8
? print cube 3
27
?
Notice that because cube is an operation it produces an output which must then be input to print. This is very important! If we want to use our procedures tan or cube in calculations (e.g. of a side length), they must output a value which can be used as input to sum, product, forward, etc. Displaying the value on the screen is no help to forward.

Practice with Operations

Write down your favorite mathematical function on a piece of paper. Then try to write a logo program which accepts an input and outputs the result of applying that function to the input.

When you've finished, turn to help your neighbor. When both of you have finished, copy each others' functions and use Logo to compose the two functions. You should be able to do this using a command like:

? print function2 function1 7
2.76534619035
?
Can you write a procedure whose output is the derivative of your favorite functin?

Can you and your partner write a procedure to find the derivative of your composition of functions? Recall that:

(f g)'(x) = f'(g(x)) g'(x).