A variable is a name for a remembered value.
You can create your own variables using =
. You need to
define your variables before you can ask the comptuer to
use them.
Once you have defined a variable, you can refer to the value by name anywhere in your program.
x = 2.875 write x * x * x
The single equals =
assigns or changes a variable value.
Be sure to not try to use =
to test equality, because it
will change your variable instead of testing it. Tests
are done with is
.
A variable can also remember any word or other sequence of characters, called a string.
song = "GEC" write song play song + song
Here song
is a name for the sequence of letters "GEG"
.
Notice that song
is unquoted and "CEG"
is quoted.
If we wanted to literally write the word "song", then we would
have had to use quotes: write "song"
.
A quoted value is called a literal value: it should not be confused with a variable name, which is unquoted.
Built-in names such as as red
,
pen
, dot
,
play
, Turtle
,
and write
, are variables that
are predefined by the system before your program runs.
It is best to avoid using these names for your own variables, because when you redefine these variables, their built-in meaning may become unavailable in your program.
Variables can take on values that are more complex than numbers and strings: they can also be arrays, objects, or functions.