Variables

Variables in SeaTurtle are like aliases (names) for numbers. What makes them powerful is that what number a variable aliases can change, whereas the code that uses the variable does not need to change. All SeaTurtle variables are global, meaning the same variable can be accessed from anywhere in your program. And since all numbers in SeaTurtle are integers, all variables in SeaTurtle represent integers as well.

Creating Variables

Variables are created by writing a statement that starts with the SET keyword, is followed by the name for the variable and then finished with a number representing what we want to set the variable to. For instance, the statement SET x 5, creates a new named number called x with the value of 5. The following program draws a polygon that has its number of sides and the length of each side determined by a variable:

SET length 50
SET sides 6
SET length 50
REPEAT sides
    FORWARD length
    RIGHT (360 / sides)
END

Notice how in in the preceding example, the variables not only make the code more clear to read, they also allow a value to be set once but used multiple times. The variable sides is set once, but used in two places. If we want to change the number of sides of the polygon, we can just change the sides variable to a different number where it is set without having to change any of the other code.

Naming Variables

Variables can be named using any combination of the letters in the latin alphabet(a-z) and numbers. No "special" characters are allowed in a variable name, such as underscores and non-latin characters. No spaces are allowed in a variable name. A variable name must start with a letter.

Variables should have descriptive, obvious names. Following this principle will maximize the readability of your code.

Updating Variables

An already existing variable can have its value changed with the same syntax as was used to create it. The SET keyword, is followed by the name for the variable and then finished with a number representing what we want to change the variable's value to. In the following example, we use the ability to change a variable to draw a kind of spiral. Notice how the length is set to its last value + 10 (incremented by 10):

SET length 10
REPEAT 20
    FORWARD length
    RIGHT 57
    SET length (length + 10)
END

Inspecting Variables

During the running of your program variables can be inspected in the "Variables" window. You can open the Variables window using the button labeled "Variables" in the toolbar. The table of variables and their respective values will live update as your program runs or is stepped-through.