Turtle Fun - Lesson Ten

This idea of a procedure calling itself as we saw in the spiral is incredibly powerful. The idea is called recursion. Someone once said, "To define recursion, you first have to define recursion." which really sums it up!

Of course, it is not enough to just "recurse", you must always make sure there is a way for the procedure to stop otherwise it will go on forever!

Here's another example:

to pattern3 :distance :angle
  forward :distance
  right :angle
  if heading = 0 [stop]
  pattern3 :distance :angle
end
The heading of the turtle tells you which way it's facing. It starts out as zero. After right 90, the heading is 90 and so on up to 360. Can you work out what
pattern3 100 90
will produce? Check.

Exercise 18 Try different numbers with pattern3. This is one of my favourites.