Scripting 3.2. Numeric variables

In any general procedural programming language you can work with variables, which are places in your computer's memory where you can store a number or anything else.

For instance, you could put the number 3.1 into the variable `b` in the following way:

`b = 3.1
`

This statement is called as assignment, i.e., you assign the value 3.1 to the variable `b`. We read this statement aloud as “`b` becomes 3.1” (or “`b` gets 3.1”, but not “`b` is 3.1”). What this means is that after this statement, the memory location `b` contains the numeric value (number) 3.1.

You can regard a variable as a box: you put the value 3.1 into the box named `b`. Or you can regard a variable as a house: the house is called `b` and now the family “3.1” is living there. Or you can regard it as any other storage location.

To see what value a variable contains (what’s in the box, or who lives in the house), you can use the `writeInfoLine` function:

`b = 3.1
` `writeInfoLine: “The value is ”, b, “.”
`

This will put the text “`The value is 3.1.`” into the Info window, as you are invited to verify.

A variable is called a variable because it is variable, i.e. its value can change. Try the script

`b = 3.1
` `b = 5.8
` `writeInfoLine: “The value is ”, b, “.”
`

You will see that `b` ends up having the value 5.8. The first line puts the value 3.1 there, but the second line replaces it with 5.8. It’s like taking the 3.1 out of the box and putting the 5.8 in its stead. Or the family 3.1 moves from the house, and the family called 5.8 moves in.

In an assignment, the part to the right of the “becomes” sign (the “=” sign) doesn’t have to be a number; it can be any formula that evaluates to a number. For instance, the script

`b = 3.1 * 2
` `writeInfoLine: “The value is ”, b, “.”
`

puts the text “`The value is 6.2.`” into the Info window. This works because Praat handles the first line in the following way:

  1. the formula `3.1 * 2` is evaluated (i.e. its value is computed), and the result is 6.2.
  2. the value 6.2 is subsequently stored in the variable `b`.

After line 1 has been executed, the variable `b` just contains the value 6.2, nothing more; the variable `b` doesn’t remember that that value has been computed by multiplying 3.1 with 2.

Formulas can contain more things than numbers: they can also contain other variables:

`b = 3.1
` `c = b * 2
` `writeInfoLine: “The value of b is ”, b, “, and the value of c is ”, c, “.”
`

In the first line, `b` gets the value 3.1. In the second line, the formula `b * 2` first has to be evaluated. Praat looks up the value of `b` (which is 3.1), so that it knows that the formula actually means `3.1 * 2`. Praat evaluates this formula and stores the result (namely the value 6.2) into the variable `c`, which will then contain nothing else than the value 6.2. The Info window thus reports “`The value of b is 3.1, and the value of c is 6.2.`”.

After these explanations, consider the following script:

`b = 3.1
` `c = b * 2
` `b = 5.8
` `writeInfoLine: “The value of c is ”, c, “.”
`

Can you figure out what the Info will report? If you think it will report “`The value of c is 6.2.`”, then you are correct: after the first line, `b` contains the value 3.1; after the second line, the value of `c` is therefore 6.2, and nothing more; after line 3, the value of `b` has changed to 5.8, but the value of `c` hasn’t changed and is still 6.2.

If you thought that `c` would end up having the value 11.6, then you’re thinking in terms of a non-procedural language such as Prolog; you may have thought that the thing assigned to `c` in the second line is the whole formula `b * 2`, so that `c` changes when `b` changes. But this is not the case: the thing stored in `c` is just the value of the formula `b * 2` at that moment, which is 6.2, and `c` doesn’t remember how it got that value. If you have trouble understanding this, consult anybody who writes programs.

### Links to this page

- Scripting
- Scripting 3. Simple language elements
- Scripting 3.4. String variables
- Scripting 5.1. Variables
- Scripting 5.7. Vectors and matrices