We have used REPEAT to perform a task more than once. Another technique is simply to place the name of the function in the last line of the function itself so that it begins all over again. For example:
TO WALK
FD RANDOM 20
LT RANDOM 360
SETPC ((RANDOM 100) + 1)
WALK
END

There are two new things here.
One is the use of the primitive 'RANDOM num' which picks a random number between 0 and num.
The other is the use of 'SETPC colour' to change the colour of the trail in the middle of a function. The WALK function sets the pen colour to a random value. There is no colour zero so we add 1 to the value returned by RANDOM to ensure that it is never 0 - the brackets in the expression ((RANDOM 100) + 1) ensure this.
The technique of calling a function from within itself is very powerful. It is called 'recursion'. Here is another recursive function:
TO SQUARE :LENGTH
REPEAT 4 [FD :LENGTH RT 90]
SQUARE :LENGTH
END
Enter 'SQUARE 50' and watch the turtle go! The function runs forever but the turtle never moves from tracing out the same four sides.
The only way to stop this program is to press the Escape key, which is not very satisfactory, it would give us more control if we had a way of stopping the process at a point that we decide in advance - in other words we would like to control the process. Here is what we do:
TO SQUARE :LENGTH :COUNTER
REPEAT 4 [FD :LENGTH RT 90]
IF :COUNTER >= 4 [STOP] [SQUARE :LENGTH :COUNTER + 1]
END
Here we are using an IF statement to control the behaviour of the program. We add a second variable to the function to count the number of times we have drawn the square and we add 1 to this value each time we call the function. A square needs only 4 sides so we test for a value >= 4. If the COUNTER has reached 4 the action taken is STOP, otherwise the action taken is to call the function again with the same value of LENGTH and COUNTER + 1.
We can make things a little more interesting by changing one small thing:
TO SQUARE :LENGTH
REPEAT 4 [FD :LENGTH RT 90]
SQUARE :LENGTH + 5
END

Do you see the change? This time the value going into each new call of SQUARE is a little bigger than the last time, by 5 in fact.
Again we can take control of this function with an IF statement:
TO SQUARE :LENGTH
REPEAT 4 [FD :LENGTH RT 90]
IF LENGTH >= 400 [STOP] [SQUARE :LENGTH + 5]
END
Notice how the actions which result from the IF statement are in square brackets. In this case LENGTH is increased with each call to the function so we can test its value in the IF statement, we do not need an additional value.
Can you make this function more interesting still? Try changing the length of a side of the square to a random value, and then try making the angle random as well.
TO SQUARE :LENGTH
REPEAT 4 [FD RANDOM :LENGTH RT 90]
SQUARE :LENGTH
END
Making the angle random does not produce a very satisfying image.

Here is some code which incorporates many of the techniques we have covered so far:
TO WORM :A :B :CThings to try: change the last line to:
WORM :A :B :C+1
Change the other variables too in similar ways.
Change the angles from 90 degrees to other values such as 60 or 37, etc.
Change the code to:
TO WORM :A :B :C :ANGLENow try changing the value of ANGLE each time the procedure is called:
TO WORM :A :B :C :ANGLE + 1
In the next section we will see how to produce more regular patterns using similar techniques.