next up previous index
Next: Read-Only variables Up: User guide Previous: Annotations

Variables

VarDeclaration ::=
    Ident ";" |
    Ident ":=" Expression
VarDeclarationList ::=
    VarDeclarationList "," VarDeclaration |
    VarDeclaration
VarDeclarations ::= "VAR" VarDeclarationList ";"

RainCode's variables must be declared before use. There is no such thing as an implicit declaration of a variable. On the other hand, since a RainCode variable can hold values of different types in the course of its lifetime, there is no attached type declaration.

VAR
  i, j, k;

RainCode supports variables that are local to a procedure, that are created when the procedure is invoked and freed when the execution of the procedure terminates. A new set of variables is created for every invocation. Consequently, RainCode supports recursion. A procedure can call itself directly or indirectly.

RainCode also supports global variables, that are created when the script is started, and that are not freed before its execution terminates. While the body of a procedure cannot access another procedure's local variables, the entire script can access the global variables.

  A variable declaration can include an initializing expression, as in:

VAR
  i := 0, Max := 100;

This expression does not commit the variable's type in the future. Initializing a variable with an integer initializing expression does not forbid the variable from denoting strings, booleans or lists afterwards. It is merely a way of avoiding the coding of tedious and error-prone initialization statements.

When dealing with global variables, the initialization expression is evaluated once when the script is started, while they are evaluated on each procedure entry when considering local variables instead.

The initialization expression can use the values of all variables and constants defined so far. The following declaration:

VAR
  Delta := 0.1, 
  Delta2 := Delta * Delta;

is valid, while

VAR
  Delta := 0.1, 
  DeltaPrime := Delta + Epsilon,
  Epsilon := 0.00001;

is invalid because the initializing expression for DeltaPrime refers to Epsilon that has not been defined yet.

If no initializing expression is defined, variables received the default value VOID,



 
next up previous index
Next: Read-Only variables Up: User guide Previous: Annotations