Turtle LibreLogo
Tilbake til startsida
 

 

Variables

In LibreLogo, as in other programming languages, it is possible to use variables. A variable is a letter or a word that can contain different values. Some of the variables are predefined, the default variables, others you can define yourself.

Default variables

These are variables that are defined in advance with a specific value or specific function. It is possible to change the value in the default variables, but it’s usually not wise to do so. Think of them as commands.

ALL

This variable returns one random value of all possible values ​​in an expression. If for example you would like to use a randomly chosen pen color, you choose the color with the command PENCOLOR ALL. This will give the pen one of all possible colors. Or perhaps you want to draw a random shape:
REPEAT 10 [ FORWARD 50 RIGHT ALL ].

TRUE and FALSE

These are so-called logical values ​​and are used in all cases where there is a need to compare values. IF :count % 2 = TRUE [ RIGHT 90 ] [ LEFT 90 ].

PAGESIZE

Returns the size in pixels of the page you paint on. PRINT PAGESIZE might return back [595.303937007874, 841.8897637795275].

PI / π

Returns the value of Pi (π) as 3.14159265359.

Customizing variables

You can also define your own variables that can be used in the entire program or in specific functions. In many Logo variants the name of the variable must begin with a colon (:). In LibreLogo the colon may be omitted, but it is a good rule to use it to show that this is a variable and not a procedure. Names of variables distinguishes between lower and upper case letters. “:name” is not the same as “:Name”.

In LibreLogo variables are defined without type. This means that variables can contain either numbers, text or lists, but not several types at the same time. One variable may contain a number the first time it is defined, and the next time a text.
:a = 3 PRINT :a * 3; returns 9.
:a = 'Hurray! ' PRINT :a * 3; will print Hurray! Hurray! Hurray!

Variables must be defined with a value before you can use them. PRINT :x will return an error message if :x is not defined in advance. Example:
:x = 5
PRINT :x
.

Example using customized variables: :length = 20, :width = :length * 2.

Global variables

Common variables are only valid in the procedure they are defined in. Sometimes it can be useful for variables to be valid in the entire program. These are defined with the word GLOBAL and the name of the variable. A global variable must first be defined before it is possible to give it a value. If a the contents of a global variable are changed somewhere, the new value be valid until it gets changed again.

Changing a global variable inside a procedure differs slightly from the way it is done in other programming languages:

GLOBAL :angle
:angle = 45

TO direction
  GLOBAL :angle
  :angel = :angle * 2
  IF :angel > 360 [ :angle = 45 ]
END

REPEAT 10 [
  BACK 50 RIGHT :angle
  direction
]

In the first line :angle is defined as a global variable. The value of this variable is set in the second line. TO direction define a procedure with the name direction. To be able to change the value of global variable inside the procedure, you must tell the program that :angle is a global variable. Then you can give it a new value with :angle = :angle * 2 (multiply the value by 2). To keep the value less than the 360 degrees of a full circle, it is set back to 45 if the value becomes greater than 360.

The call of the function occurs in the function REPEAT. First, the command is executed, then :angle is given a new value. This new value is used the next time the command is executed.


© Context and design: Kolbjørn StuestølStuestøl homesite (in Norwegian)Modified December 6 2015