Learn creative coding writing simple programs

134. Variable names and types in SuperCollider

In other languages you need to declare your variables before using them, and you need to declare the type of the variable (that means you need to say if you plan to store a number or some text in a variable). In SuperCollider you don't specify the type because it automatically knows.

Variable names have something special about them in SuperCollider. In other languages you can use any name for your variable (as long as it's not a reserved word like while, for or if). SuperCollider distinguishes three types of names:

1) names which are one letter long, like a, b, c. These are global variables and can be used any time. The variable s is special, and it's predefined. It contains the local audio server, so don't use s. Use any other one letter variable between a and z.

2) names that begin with tilde (~). These are also global variables and don't need to be declared, and can be long, like ~width, ~height, ~theSizeOfMyNose.

3) long names that don't begin with ~, like width and height. It's only possible to use such names inside a region or a function, and the variable disappears at the end of the region or the function. These variables must be declared at the beginning of the function or the region, using they keyword var or arg (we will see functions and arg in coming episodes). An example of declaring such variables: var myWidth, myHeight;

Tags: supercollider, variable, global, type, int, float, array, string

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
  Do `float` and `int` smell similar?
To highlight code blocks, use ```processing and ``` like this:
  void setup() {
    size(600, 600);