# &kturtle;'s &logo; Programming Reference

## Commands

Using commands you tell the turtle or &kturtle; to do something. Some commands need input, some give output. In this section we explain all the commands that can be used in &kturtle;.

### Moving the turtle

There are several commands to move the turtle over the screen.

forward

-     forward X

  `forward` moves the turtle forward by the amount of X pixels. When the pen is down the turtle will leave a trail. `forward` can be abbreviated to `fw`

backward

-     backward X

  `backward` moves the turtle backward by the amount of X pixels. When the pen is down the turtle will leave a trail. `backward` can be abbreviated to `bw`.

turnleft

-     turnleft X

  `turnleft` commands the turtle to turn an amount of X degrees to the left. `turnleft` can be abbreviated to `tl`.

turnright

-     turnright X

  `turnright`the turtle to turn an amount of X degrees to the right. `turnright` can be abbreviated to `tr`.

direction

-     direction X

  `direction` set the turtle's direction to an amount of X degrees counting from zero, and thus is not relative to the turtle's previous direction. `direction` can be abbreviated to `dir`.

centre

-     centre

  `centre` moves the turtle to the centre on the canvas.

go

-     go X,Y

  `go` commands the turtle to go to a certain place on the canvas. This place is X pixels from the left of the canvas, and Y pixels form the top of the canvas. Note that using the `go` command the turtle will not draw a line.

gox

-     gox X

  `gox` using this command the turtle will move to X pixels from the left of the canvas whilst staying at the same height.

goy

-     goy Y

  `gox` using this command the turtle will move to Y pixels from the top of the canvas whilst staying at the same distance from the left border of the canvas.

### The turtle has a pen

The turtle has a pen that draws a line when the turtle moves. There are a few commands to control the pen. In this section we explain these commands.

penup

-     penup

  `penup` lifts the pen from the canvas. When the pen is up no line will be drawn when the turtle moves. See also `pendown`. `penup` can be abbreviated to `pu`.

pendown

-     pendown

  `pendown` presses the pen down on the canvas. When the pen is press down on the canvas a line will be drawn when the turtle moves. See also `penup`.`pendown` can be abbreviated to `pd`.

penwidth

-     penwidth X

  `penwidth` sets the width of the pen (the line width) to an amount of X pixels. `penwidth` can be abbreviated to `pw`.

pencolour

-     pencolour R,G,B

  `pencolor` sets the color of the pen. `pencolor` takes an RGB combination as input. `pencolor` can be abbreviated to `pc`.

### Commands to control the canvas

There are several commands to control the canvas.

canvassize

-     canvassize X,Y

  With the `canvassize` command you can set the size of the canvas. It takes X and Y as input, where X is the new canvas width in pixels, and Y is the new height of the canvas in pixels. `canvassize` can be abbreviated to `cs`.

canvascolour

-     canvascolour R,G,B

  `canvascolor` set the color of the canvas. `canvascolor` takes an RGB combination as input. `canvascolor` can be abbreviated to `cc`.

wrapon

-     wrapon

  With the `wrapon` command you can set wrapping on for the canvas. Please see the glossary if you want to know what wrapping is.

wrapoff

-     wrapoff

  With the `wrapoff` command you can set wrapping off for the canvas: this means the turtle can move off the canvas and can get lost. Please see the glossary if you want to know what wrapping is.

### Commands to clean up

There are two commands to clean up the canvas after you have made a mess.

clear

-     clear

  With `clear` you can clean all drawings from the canvas. All other things remain: the position and angle of the turtle, the canvascolor, the visibility of the turtle, and the canvas size. `clear` can be abbreviated to `ccl`.

reset

-     reset

  `reset` cleans much more thoroughly than the `clear` command. After a `reset` command everything is like is was when you had just started &kturtle;. The turtle is positioned at the middle of the screen, the canvas color is white, and the turtle draws a black line on the canvas.

### The turtle is a sprite

Many people do not know what sprites are, so here a short explanation: sprites are small pictures that can be moved around the screen. (for more info see the glossary on sprites). So the turtle is a sprite.

Next you will find a full overview on all commands to work with sprites.

[The current version of &kturtle; does not yet support the use of sprites other than the turtle. With future versions you will be able to change the turtle into something of your own design]

show

-     show

  `show` makes the turtle visible again after it has been hidden.`show` can be abbreviated to `ss`.

hide

-     hide

  `hide` hides the turtle. This can be used if the turtle does not fit in your drawing.`hide` can be abbreviated to `sh`.

### Can the turtles write text?

The answer is: yes. The turtle can write: it writes just about everything you command it to.

print

-     print X

  The `print` command is used to command the turtle to write something on the canvas. `print` takes numbers and strings as input. You can `print` various numbers and strings using the + symbol. See here a small example:

      year = 2004
      author = "Cies"
      print "KTurtle was made in " + year + " by " + author

fontsize

-     fontsize X

  `fontsize` sets the size of the font that is used by `print`. `fontsize` takes one input which should be a number. The size is set in pixels.

### A command that rolls a dice for you

There is one command that rolls a dice for you

random

-     random X,Y

  `random` is a command that takes input and gives output. As input are required two numbers, the first (X) sets the minimum output, the second (Y) sets the maximum. The output is a randomly chosen number that is equal or greater then the minimum and equal or smaller than the maximum. Here a small example:

      repeat 500 [
        x = random 1,20
        forward x
        turnleft 10 - x
      ]

  Using the `random` command you can add a bit of chaos to your program.

## Containers

Containers are letters or words that can be used by the programmer to store a number or a text. Containers that contain a number are called variables, containers that can contain text are called strings.

Containers that are not used yet are 0 by default. An example:

    print N

This will print a 0.

### Variables: number containers

Let us start with an example:

    x = 3
    print x

In the first line the letter `x` made into a variable (number container). As you see the value of the variable `x` is set to 3. On the second line the value is printed.

Note that if we wanted to print an x that we should have written

    print "x"

That was easy, now a bit harder example:

    A = 2004
    B = 25
    AB = A + B

    # the next command prints "2029"
    print AB
    backward 30
    # the next command prints "2004 plus 25"
    print "" +  A + " plus " + B
    backward 30
    # the next command prints "1979"
    print A - B

In the first two lines the variables `A` and `B` are set to 2004 and 25. On the third line the variable `AB` is set to `A + B`, which is 2029. The rest of the example consists of 3 `print` commands with `backward 30` in between. The `backward 30` is there to make sure every output is on a new line. In this example you also see that variables can be used in mathematical calculations.

### Strings: text containers

Strings are a lot like variables. The biggest difference is that strings cannot be used in mathematical calculations and questions. An example of the use of strings:

    x = "Hello "
    name = inputwindow "Please enter your name..."
    print x + name + ", how are you?"

On the first line the string `x` is set to Hello . On the second line the string `name` is set to the output of the `inputwindow` command. On the third line the program prints a composition of three strings on the canvas.

This program ask you to enter your name. When you, for instance, enter the name Paul, the program prints Hello Paul, how are you?. Please note that the plus (+) is the only math symbol that you can use with strings.

## Can the Turtle do maths?

Yes, &kturtle; will do your math. You can add (+), substract (-), multiply (*), and divide (/). Here is an example in which we use all of them:

    a = 20 - 5
    b = 15 * 2
    c = 30 / 30
    d = 1 + 1
    print "a: "+a+", b: "+b+", c: "+c+", d: "+d

Do you know what value a, b, c and d have?

If you just want a simple calculation to be done you can do something like this:

    print 2004-12

Now an example with brackets:

    print ( ( 20 - 5 ) * 2 / 30 ) + 1

The expressions inside brackets will be calculated first. In this example, 20-5 will be calculated, then multiplied by 2, divided by 30, and then 1 is added (giving 2).

## Asking questions, getting answers...

`if` and `while` are execution controllers that we will discuss in the next section. In this section we use the `if` command to explain questions.

A simple example of questions:

    x = 6
    if x > 5 [
      print "hello"
    ]

In this example the question is the `x > 5` part. If the answer to this question is true the code between the brackets will be executed. Questions are an important part of programming and often used together with execution controllers, like `if`. All numbers and variables (number containers) can be compared to each other with questions.

Here are all possible questions:

## Types of questions

`a == b` | equals | answer is true if `a` equals `b`
`a != b` | not-equal | answer is true if `a` does not equal `b`
`a > b` | greater than | answer is true if `a` is greater than `b`
`a < b` | smaller than | answer is true if `a` is smaller than `b`
`a >= b` | greater than or equals | answer is true if `a` is greater than or equals `b`
`a <= b` | smaller than or equals | answer is true if `a` is smaller than or equals `b`

Questions can also be glued to each other with question glue, this way a few questions can become one big question.

    a = 1
    b = 5
    if a < 5 and b == 5 [
      print "hello"
    ]

In this example the glue-word `and` is used to glue 2 questions (`a < 5`, `b == 5`) together. If one side of the `and` would answer false the whole question would answer false, because with the glue-word `and` both sides need to be true in order to answer true.

`and` is not the only glue-word there are two others. They are all in the next table:

## Question glue-words

`and` | both sides need to be true in order to answer true
`or` | if one of the sides is true the answer is true
`not` | only if both of the sides are false the answer is false

## Controlling execution

The execution controllers enable you — as their name implies — to control execution.

### Have the turtle wait

If you have done some programming in &kturtle; you have might noticed that the turtle can be very quick at drawing. This command makes the turtle wait for a given amount of time.

wait

-     wait X

  `wait` makes the turtle wait for X seconds.

      repeat 36 [
        forward 5
        turnright 10
        wait 0.5
      ]

  This code draws a circle, but the turtle will wait half a second after each step. This gives the impression of a slow-moving turtle.

### Execute "if"

if

-     if question [ ... ]

  The code that is placed between the brackets will only be executed `if` the answer to the question is true. Please read for more information on questions in the question section.

      x = 6
      if x > 5 [
        print "x is greater than five!"
      ]

  On the first line `x` is set to 6. On the second line the question `x > 5` is asked. Since the answer to this question is true the execution controller `if` will allow the code between the brackets to be executed

### Execute "while"

while

-     while question [ ... ]

  The execution controller `while` is a lot like `if`. The difference is that `while` keeps repeating the code between the brackets till the answer to the question is false.

      x = 1
      while x < 5 [
        forward 10
        wait 1
        x = x + 1
      ]

  On the first line `x` is set to 1. On the second line the question `x < 5` is asked. Since the answer to this question is true the execution controller `while` starts executing the code between the brackets till the answer to the question is false. In this case the code between the brackets will be executed 4 times, because every time the fifth line is executed `x` increases by 1..

### If not, in other words: "else"

else

-     if question [ ... ] else [ ... ]

  `else` can be used in addition to the execution controller `if`. The code between the brackets after `else` is only executed if the answer to the question that is asked is false.

      x = 4
      if x > 5 [
        print "x is greater than five!"
      ] else [
        print "x is smaller than six!"
      ]

  The question asks if `x` is greater than 5. Since `x` is set to 4 on the first line the answer to the question is false. This means the code between the brackets after `else` gets executed.

### The "for" loop

for

-     for start point to end point [ ... ]

  The `for` loop is a counting loop, &ie; it keeps count for you.

      for x = 1 to 10 [
        print x * 7
        forward 15
      ]

  Every time the code between the brackets is executed the `x` is increased by 1, till `x` reaches the value of 10. The code between the brackets prints the `x` multiplied by 7. After this program finishes its execution you will see the times table of 7 on the canvas.

## 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 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 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.