docs.kde.org
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 200,200 
circle 20

go 300,200 
circle 40  

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

reset

learn multiplyBySelf $n {
  $r = $n * $n
  return $r
}
$i = ask "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.

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.

Prev
Next
Home


docs.kde.org