Create your own commands with “learn
Prev
Next

Create your own commands with “learn

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 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, a number, 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 30,30 
circle 20

go 40,40 
circle 50  

In the next example, a command with a return value is created.

reset

learn multiplyBySelf n [
  r = n * 1
  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 itself and then returned, using the return command. The return command is the way to output a value from a function you have created.

Prev
Next
Home


Would you like to make a comment or contribute an update to this page?
Send feedback to the KDE Docs Team