## Writing procedures with input, variables

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.

### Special Operations

Logo operates according to rigid rules, but its designers programmed a few exceptions to those rules for our convenience. One example is our ability to use parentheses to change the number of inputs certain procedures require:
```(print "Happy word sum 13 5 "th "Birthday, "yo!)
```
Another example is that Logo allows use of standard arithmetic symbols:
```print 2 + 3 * 5
```
What value do you think the instruction above will display? How would you draw a plumbing diagram for it?
```print sum 2 product 3 5
```
is less ambiguous, but you might find 2 + 3 * 5 easier to read.

### Review

We've covered all the material in Chapter 2 (except procedure count), but could use a little more practice understanding how computers think before moving on.

Create plumbing diagrams for as many of these instructions as possible in the time provided:

```music.quiz
print product 5 10
print word sum 2 4 "es
repeat 4 [forward 100 right 90]
print count [The quick brown fox]
(print "How "are "you "feeling?)
print word word last "awful first butfirst "computer first [go to the store, please.]
```
Check your work by using help and the plumbing diagram to predict Logo's response to each instruction, then comparing your prediction to what actually happens when you evaluate the instruction in Logo.

### Done With Chapter 2?

We're about to move on to Chapter 3. Do you have any questions about Chapter 2 before we do?

### Chapter 3

Now that we understand the structure of a Logo procedure and of a basic instruction, we can start writing more interesting procedures.

#### Procedures with Input

So far, all the procedures we've written have very boring plumbing diagrams. They're commands -- they don't have a "downspout", and they don't have any "hoppers" on top for input.

How do we write a procedure that accepts input?

You might be thinking of the operation readlist we used in Chapter 2, but that requires us to type in the required information after a procedure has started running. We can, and often should, make the computer input information for us!

On page 39, the author writes a procedure greet to say hello to a person. Let's do the same.

Remember, writing a new procedure defines a new vocabulary word for Logo. We start our procedure definition at the question mark prompt with the word to. After to comes the name of the procedure, greet.

Our procedure greet will require one input -- the name of the person it's saying hello to. We have to somehow tell Logo that greet requires one input. We also provide some information about where we want this input to be stored. To do this, we include a variable name after the name of the procedure in the procedure definition:

```? to greet :person
>
```

The author suggests that you think of the word person here as the name of a container that will hold some data. Notice that it's correct form to put punctuation (a colon :) in front of person.

#### A procedure with input, and thing

The entire procedure looks like:

```to greet :person
print sentence "Hello, thing "person
end
```
(Notice that I've omitted ? and > to make the procedure easier to copy and paste. I've also indented the print statements to make it easier to see where the procedure starts and ends.)

thing is a Logo operation which needs one input -- the name of a variable or container -- and which outputs the value stored in the variable. This idea of retrieving information from storage is central to computer use and programming. Because we want to look up the value stored in a container frequently, we can abbreviate thing "varname to :varname.

#### How greet works

When procedure greet is run, the first thing Logo does is set aside a storage space labeled person and put the input to greet in that storage space. Then Logo evaluates the instruction print sentence "Hello, thing "person, which requires it to look up the value stored in that variable.

Exactly what happens when Logo evaluates print sentence "Hello, thing "person? See page 41 of the Chapter 3 for the plumbing diagram. The author recommends that you expand : to thing " when drawing plumbing diagrams.

#### Using greet in an insruction

How can we run the procedure greet? In a complete instruction, a command is followed by some needed expressions. How many expressions does greet need? How do we give it those expressions?

```? greet "Heidi
Hello Heidi
?
```

Run procedure greet a few times, then ask yourself how you'd use the rules from page 21 in Chapter 2 to describe procedure greet:

• Is greet a command or operation?
• How many inputs does greet require?
• What type of inputs does it expect?
• What is the effect of greet?
• Draw a plumbing diagram for the instruction greet "Heidi

If you have free time, see if you can write a procedure like which accepts a noun as input and then prints a message like "I like nouns".

```? like "dog
I like dogs.
? like "cat
I like cats.
? like "beer
I like beers.
? like "COMP203
I like COMP203s.
?
```

#### What is a Variable, Really?

You've all passed precalculus, so you probably think you understand variables. If you expect the Logo variable person to be the same as the precalculus variable x, you will be disappointed (and probably frustrated). In this class you will never be asked to solve for the value of person (though you may be asked what value is stored there!) However, you should frequently find yourself wondering what happens if person has this or that value. Experiment with procedure greet to see what happens when person stores different values. Can the value of person be a full name? a nickname? A number? A hyphenated name?

What is a variable in Logo? The article The Many Uses of Algebraic Variables lists several different roles variables can play. Consider the equation y = mx + b. You know from experience that x is an input or varying value, m and b are parameters describing the slope and y-intercept of a line, and y is an output or perhaps an unknown. In the equation y = ex, e isn't variable at all -- it's a constant like pi. In the equation PV = nRT, the variable R is a constant like e and any of the other variables could be unknown or determined by physical measurement.

Variables in Logo are like the argument x in the function definition f(x) = 2x + 4. The Logo equivalent of evaluating f(x) at x=2 is to store the value 2 in variable x and then run procedure f. We can then use the resulting output to describe an amount or a point on a line.

The examples below are procedures which cause Logo to evaluate different functions and display the result on the screen.

```to addthreeto :x
print sum :x 3
end

to negativesquarerootof :x
print product -1 sqrt :x
end

to fancyfunctionof :x
print (sentence [The value of f at] :x [is] word 2 * :x + 4 ".)
end
```
(Soon we'll talk about ways to make this procedure output a value rather than just printing one; for now we'll write commands rather than operations.)

Try to guess what each of these procedures will do, then run them to check your work. If you're not sure what a procedure will do, draw a plumbing diagram to help you!

Try to write a procedure double which accepts a number as input and displays twice that number on the screen. When you're done, see if you can "fancy it up" to display a sentence such as the one produced by procedure fancyfunctionof.

Logo is unlike many other languages; every variable can store a number, word, list, or even list of instructions. What happens if you ask Logo to addthreeto "Heidi? Why?

The task of making sure that a variable holds the type of value we expect it to is left to Logo's procedures. Once we've learned about predicates, you can earn extra credit by writing procedures that check to be sure their input is the correct type.

Challenge: Write a procedure which requires two inputs, a number and a word, and prints a message about letters in the word as in the example below:

```? letternum 5 "competent
Letter number 5 in the word competent is e.
? letternum 3 "cat
Letter number 3 in the word cat is t.
? letternum 4 "Heidi
Letter number 4 in the word Heidi is d.
```
For more practice thinking through the steps of a procedure, writing procedures, and writing procedures with variables, start working on the procedures worksheet.