
Every procedure so far has done something - drawn a square, moved the turtle. A procedure can instead work something out and give you the answer, which is what makes it possible to build up arithmetic of your own.
op, short for output, is how a procedure gives an answer:
to double :n
op :n * 2
end
Then:
?pr double 21
42
double 21 did not print anything itself. It worked out 42 and handed it back,
and pr printed it.
op stops the procedure there and then. Anything written after it does not
run, which is worth remembering when a procedure has more than one line.
Because double gives back a number, it can be used anywhere a number is
wanted - including as the input to another procedure, or to itself:
?pr double double 5
20
The inner double 5 gives 10, and that becomes the input to the outer one.
This is where procedures stop being shorthand and start being building blocks.
Leave op off and the procedure works the value out and then has nowhere to put
it:
?pr noop 5
I don't know what to do with 5 in noop:
:n * 2
Dr Logo names the procedure and prints the line it stopped on underneath, which is the most helpful complaint it makes.
to double :n
op :n * 2
end
pr double 21
pr double double 5
Starting from
A fresh boot, at the ? prompt.
What you should see
double defined
42
20

Remember the * is SHIFT and the : key. If you need a minus sign anywhere,
it is SHIFT and the = key - the key marked - does not give one.
double 21 on its own, with no pr in front. Something is printed
anyway. What does that suggest about what the ? prompt does with a value
nobody has asked for?op out, leaving just :n * 2, and run pr double 21. Read the
complaint carefully - it tells you both which procedure and which line.quadruple that uses double twice rather than multiplying by four.
Which of the two would you rather change if double turned out to be wrong?| Symptom | Cause |
|---|---|
I don't know what to do with ... in ...: |
The procedure worked something out and never said op. |
Not enough inputs to double |
You called it without a number. |
The procedure prints nothing and pr complains |
pr needs something handed to it. op is what hands it over. |
Lines after the op never seem to run |
They do not. op ends the procedure. |
I don't know how to _ |
You used the key marked -. The minus sign is SHIFT and =. |
op hands a value back out of a procedure and ends it there. Anything that
outputs can be used where a value is wanted, including as another procedure's
input. A procedure that computes and never says op is an error, and Dr Logo
tells you which line.
S14, a procedure that calls itself - which sounds like a mistake and is one of the most useful things in the language.