Next: Simple types Up: Exploring the Basics Previous: Comments

# Variables

Variables allow users to assign names to the results of expressions. Suppose you would like to calculate a long series of arithmetic expressions and some of the values in these expressions are used more than once. For instance, instead of the rather laborious typing exercise of

```> 5 * 83945773897929834;
> 7 * 83945773897929834;
> 9 * 83945773897929834;
```
one could instead assign this rather cumbersome number to a variable and use it instead in subsequent multiplications.
```> num := 83945773897929834;
> 5 * num;
> 7 * num;
> 9 * num;
```
We say that the Darwin variable num was assigned a value. In the above case, num was assigned a value which has type real. The real type corresponds to numbers which may have a fraction part. In general, variables may have one of several different types.
```> year := 1997;                           # a variable of type posint
> temperature := -50;                     # a variable of type integer
> pie := 3.14;                            # a variable of type real
> decision := true;                       # a variable of type boolean
> name := 'darwin';                       # a variable of type string
```
Table  lists some of the basic types in Darwin.

There are rules in Darwin for what constitutes valid variable names. These are as follows:

• Variables must either begin with a letter ( ) or the underscore symbol (`_`). Subsequent symbols may be letters, the underscore symbol (`_`) or numbers ().
• The length of a variable name must be less than approximately 240 symbols. This bound is system dependent.
• Darwin distinguishes between uppercase and lowercase letters. Thus, variable Result is different from variable result.
• The use of the underscore symbol (`_`) to begin variable names is discouraged since the Darwin system uses this convention when creating system variables. If you are unlucky, you may assign a new value to a system variable or the system may assign a new value to your variable. In either case, you probably will not appreciate the resulting havoc.
The following are all legal variable names in Darwin:
```> this_is_the_longest_variable_name_in_the_world := 'big';
> R2D2 := true;
> _toying_with_disaster := 'the dangerous underscore';
```

A word of advice from the computer science community: always use variables names that are meaningful in that they express the function of the variable in the program. This makes your code easier for others to read. (It also makes it easier for you to read when you re-examine your work decades later.)

Next: Simple types Up: Exploring the Basics Previous: Comments
Gaston Gonnet
1998-09-15