Defining a variable

In programming, a variable is basically a named bucket inside the computer’s memory that holds data.

Defining a variable in Experimentor is pretty easy:

define myVariable = 18

This defines a new variable and puts the value of 18 into that variable.  Note that with Experimentor when you define a variable you must give it a value, because all variables in Experimentor have values.  (For computer geeks out there, it is not possible to have a variable with a “null” or “Nothing” value in Experimentor – every variable has a value.)

Of course, you do not need to call your variable “myVariable”, (which is in fact a very bad and silly variable name).  You should call your variables with a name that represents what data that variable is storing for you.  So perhaps a more appropriate example would be:

define red     = #FF0000

define green = #00FF00

define blue   = #0000FF

Which defines three different variables called “red”, “green” and “blue” that each holds a colour value (in this case, the colours solid red, solid green and solid blue respectively).

To use the value inside this variable is pretty easy to – just use the name of the variable:

print(“Colour: “ + red )

You can use your variable name just about anywhere to access the value inside that “data bucket”.

For example, you can even use a variable when defining another variable:

define red = #FF0000

define favouriteColour = red

Which defines a variable “favouriteColour” and sets its value to the current value of the “red” variable.

So, with that knowledge, we can try a small experiment.  What will the following very short program print out?

define red = #FF0000

define favouriteColour = red

red = “Hello mom!”
print(“Favourite colour: “ + favouriteColour )

On line 2 the “favouriteColour” variable is assigned the current value of the “red” variable, which at that point in time is the solid colour red (#FF0000).  So on the next line setting the “red” variable to “Hello mom!” has absolutely no impact on the “favouriteColour” variable.  Conceptually, on line 2 we are copying the value from the “red” data bucket into the “favouriteColour” bucket, which is a completely separate bucket.  Then, putting a new piece of data into the “red” data bucket doesn’t affect the data still in the “favouriteColour” bucket.