next up previous contents
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:

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 up previous contents
Next: Simple types Up: Exploring the Basics Previous: Comments
Gaston Gonnet
1998-09-15