Create your own commands!
learn is a very special command, because it is used to create your own commands. The command you create can take input and return output. Let's 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, a number, to set the size of the circle.
circle returns no
output. The
circle command can now be used like a normal command. See this example:
learn circle (x) [
repeat 36 [
forward x
turnleft 10
]
]
go 30,30
circle 20
go 40,40
circle 50
In the next example a command with a return value is created.
learn multiplyBySelf (n) [
r = n * n
return r
]
i = inputwindow "Please enter a number and press OK"
print i + " multiplied by itself is: " + multiplyBySelf (i)
In this example a new command called
multiplyBySelf is created. The input of this command is multiplied by it self and then returned, using the
return command. The
return command is the way to output a value from a function you have created.