
learn
is special as it is used to create your own commands. The commands you create can take input and return output. Let us take a look at how a new command is created:
learn circle $x { repeat 36 { forward $x turnleft 10 } }
The new command is called circle
. circle
takes one input argument, to set the size of the circle. circle
returns no output. The circle
command can now be used like a normal command in the rest of the code. See this example:
learn circle $X { repeat 36 { forward $X turnleft 10 } } go 200,200 circle 20 go 300,200 circle 40
In the next example, a command with a return value is created.
learn faculty $x { $r = 1 for $i = 1 to $x { $r = $r * $i } return $r } print faculty 5
In this example a new command called faculty
is created. If the input of this command is 5
then the output is 5*4*3*2*1
. By using return
the output value is specified and the execution is returned.
Commands can have more than one input. In the next example, a command that draws a rectangle is created:
learn box $x, $y { forward $y turnright 90 forward $x turnright 90 forward $y turnright 90 forward $x turnright 90 }
Now you can run box 50, 100
and the turtle will draw a rectangle on the canvas.