Dr Logo for the Tatung Einstein, showing a rosette drawn by the turtle
← Back to Courses
module
13

Getting an answer back

Introduction

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.

Handing something back

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.

Answers as inputs

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.

What happens without it

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.

The code

Starting from

A fresh boot, at the ? prompt.

What you should see

double defined
42
20

A procedure that outputs a value, used twice

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.

Change one thing

  • Type 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?
  • Take the op out, leaving just :n * 2, and run pr double 21. Read the complaint carefully - it tells you both which procedure and which line.
  • Write a 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?

When it goes 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 =.

Summary

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.

Next

S14, a procedure that calls itself - which sounds like a mistake and is one of the most useful things in the language.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.