The concepts in this section are related to the mathematical terms with the same names. This is a modern-day result of the first uses of computers and programming languages: the calculation of complex mathematical problems.
A variable is a symbol that can be assigned an arbitrary value. A "symbol" is a series of alphabetic and numeric characters, separated by whitespace (a space, a line-break, or the end of the file). When a variable is "assigned" a value, the variable name (the symbol) is understood to be a substitute for the assigned value.
Consider a traffic light, which has three possible symbols: green, yellow, and red. When you are driving, and you encounter a traffic light, you might see that its red symbol is activated (the red light is illuminated). What you see is a red light, but you understand that it means you should stop your car. Red lights in general do not make you stop - it is specifically red traffic lights, because we know that it is a symbol meaning to stop.
SuperCollider's variables work in the same way: you tell the interpreter that you want to use a symbol, like cheese
. Then you assign cheese
a value, like 5
. After that point, whenever you use cheese
, the interpreter will automatically know that what you really mean is 5
.
Run the following two programs. They should result in the same output.
(
5 + 5;
)
(
var x;
x = 5;
x + x;
)
In the first example, the program calculates the value of 5 + 5
, which is 10
, and returns that to the interpreter, which prints it out. In the second example, the program tells the interpreter that it wants to use a variable called x
then it assigns cheese the value 5
. Finally, the program calculates cheese + cheese
, which it understands as meaning 5 + 5
, and returns 10
to the interpreter, which prints it out.
This trivial use of a variable does nothing but complicate the process of adding 5 to itself. Soon you will see that variables can greatly simplify your programs.