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

A procedure that calls itself

Introduction

A procedure can use any word in the language, and by S9 that included words you had defined yourself. There is nothing stopping a procedure using its own name.

That sounds like a mistake. It is one of the most useful things in Logo, and it is how most of the interesting pictures get drawn.

Doing a bit, then asking for the rest

Here is a spiral. Read it as an instruction to somebody else:

to spiral :size
if :size > 60 [stop]
fd :size
rt 90
spiral :size + 5
end

Draw a line this long. Turn right. Now do the whole thing again, but slightly bigger.

Each call does almost nothing - one line and one turn. The shape comes from there being a lot of them, each a little larger than the last.

The line that matters

if :size > 60 [stop]

if takes a test and a list of instructions in brackets, and runs the list only when the test is true. stop leaves the procedure immediately.

Without that line the procedure would call itself forever, because nothing would ever stop it. Every procedure that calls itself needs a way to not. It is the first thing to write, not the last, and the edits below let you see what happens when it is missing.

Notice that the condition is about :size, and that :size gets bigger with every call. That is what makes the test eventually true. A recursion whose input never changes never finishes.

The code

Starting from

A fresh boot, at the ? prompt. The > is SHIFT and the . key.

What you should see

A square spiral winding outwards from the middle, of about a dozen turns, which stops by itself.

A square spiral drawn by a procedure that calls itself

Change one thing

  • Change the 60 to 100. More turns, and the spiral runs out of screen before it runs out of numbers. Which limit stopped it?
  • Change :size + 5 to :size + 10. The spiral is the same size overall but looks quite different. Why fewer turns?
  • Take the if line out altogether and run spiral 5 again. It will not stop. Watch it for a few seconds, then press ESC. Read what it says - it tells you which procedure it was in and which line it was on.

When it goes wrong

Symptom Cause
It never stops There is no if ... [stop], or the input never reaches the test. Press ESC.
Stopped! in spiral: rt That is ESC working, naming where it interrupted.
It stops immediately and draws nothing The test was already true on the first call. Check which way round the > is.
The spiral goes off the screen Once the sides are longer than the screen the rest is drawn where you cannot see it.
I don't know how to _ The key marked - is not a minus. It is SHIFT and =.

Summary

A procedure may call itself. Each call does a small piece of the job and hands the rest on. if test [stop] is what makes it finish, and the input has to change on each call so that the test eventually becomes true. ESC rescues you when it does not.

Next

S15, lists - the other half of Logo, and the thing the square brackets have been quietly preparing you for since S3.

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.