[storage] typeName [= initializer];

The only required elements in declaring a variable are it’s identifier (name) and it’s type.  Optionally you can specify an initialization value (which is always recommended), and a storage specifier.

The only currently supported storage specifier is 'static' ( 'const' will likely be supported shortly and possibly 'extern').  A variable declared with static storage will retain it's value between function calls.  For example:

int count()
    static int i = 0;
    return(i = i + 1);

In this example the static variable 'i' is initialized to the value of 0 the first time it's called.  With each subsequent call 'i' gets incremented by one and returns that value.  Since 'i' is declared static it will remember the value of 'i' between calls so the next time it gets called it will have a running total for the number of times that count() has been called.

Global variables declared with static storage have a scope local to the script file.  Non-static globals can be referenced in other scripts and through expressions by their fully qualified name (e.g. ).  See Scope for more information.

A variable is a block of data that can be used to store and retrieve information.  If you’ve used messiah’s expressions via Command mode then you are already familiar with variable.  What it commonly referred to as an "expression" is really a variable.

Variables have three parts to them, a name, a type and a value.  When using variables through command mode we set the name through the interface, the type and value are determined by evaluating the expression. Command mode variables can by thought of as dynamic types, their type is not fixed, but rather determined by the data they refer to.

In scripting we need to declare these pieces of information explicitly, that is we need to tell the scripting engine that we want to create a variable, give it a name and a type (and possibly some initial value).

To create a string variable called 'test' with an initial value of "Hello World" we would do the following:

string test = "Hello World"; 

This provides the scripting engine with all the information it needs to be able to create this variable for use later in the script.

We've taken for granted the fact that the '=' sign is used to assign the value of one variable (or constant) to another variable.  The following are all assignments:

int i = 0;
i = someFunc();
i = j;

but how do we test equality?  You're probably aware that a < b tests whether 'a' is less than 'b'.  But if we write a = b then are we testing whether 'a' equals 'b'?  Nope.  Instead of testing equality we are assigning the value of 'b' to the variable 'a'.  Instead we need to use the '==' symbol (two equals signs). So a == b means "does 'a' equal 'b'?".

Remember:  = means assignment, == means equality.  And LOL means "laughing out loud."

Converted from CHM to HTML with chm2web Pro 2.82 (unicode)