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 stringTable lists some of the basic types in Darwin.
There are rules in Darwin for what constitutes valid variable names. These are as follows:
_
). Subsequent symbols may be letters, the
underscore symbol (_
) or numbers ().
_
) 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.
> this_is_the_longest_variable_name_in_the_world := 'big'; > R2D2 := true; > _toying_with_disaster := 'the dangerous underscore';