Turtle Academy just had a system upgrade if you experience any issues. Please try to hard refresh using CTRL + F5.

If the issues continue please contact us.

14. Recursion

In the lesson "The turtle is Learning" we saw how we can teach the turtle new words by using the command to We can also teach the turtle a new command using to followed by a parameter, such as - to advance :PARAM_NAME fd :PARAM_NAME. The turtle will now expect to receive a param (a value) after he gets the new "advance" command. For example: advance 40.
Teach the turtle what is advance with a parameter called "thesize"
As we saw in the previous step, we can teach the turtle to do actions that make him expect params (values). As we saw in previous lessons, in order to teach the turtle how to draw a square we should type the command instructions:to square repeat 4 [ fd SOME_NUMBER rt 90] end.
Teach the turtle the square command. Make it expect a "size" parameter. The "size" parameter will determine the square's size.
We have already created a new command named square that expects a parameter. Let's check that it works. You can type the command square 60 or the command square 80 and so on.
Type the command square 100
When a command calls itself, that process is named 'recursion' When using recursion we must have a stopping condition (we don';t want to have an infinite loop) and a call for a command with new parameters.Sound complicated? Not to worry, we will go over it step by step. Let's teach the turtle the 'square' command by using recursion.
Teach the turtle the following: to square :size if :size > 90 [stop] repeat 4 [ fd :size rt 90] square :size + 20
Let's see how does it works behind the scenes when we use the square 40 command. At first we call the command square 40. That means that the parameter :size inside the square command is getting a value of 40. Then we use the if condition. What we are actually doing is we are asking the turtle: Is the :size value bigger then 90 (:size > 90)? If it is then stop the program. If it isn't then follow these instructions - Draw a square with the :size parameter. Call the square with :size = :size + 20. Now the :size parameter is 60. It is still < 90 so we will draw a square that is 60 in size and call to square :size = :size + 20. Now the :size parameter is 80, which is still less then 90, so the turtle will draw a square that is 80 in size and call to square with :size=:size+20. Now :size is 100 > 90 so we will stop the program.
Type the command square 40
Let's create a spiral command for the turtle to learn. The spiral will receive 1 parameter called :size. The stopping condition will be when :size > 30. With each call we will go forward according to the size parameter, fd :size and then we will turn right 15, rt 15. The final instruction will be to call a spiral with :size * 1.02
Teach the turtle what is a spiral
TurtleAcademy learn programming for freeYour browser is not supporting canvasWe recomand you to use Chrome or Firefox browsers