Variables

Prior | Next

Declaring Variables
A variable is a name in your script that stands for a memory address, the contents of which can change as the script runs. You need to declare a variable in a var section before using it.

When you declare a variable, select a name that reminds you of the variable's purpose, thus making your script easier to read. For example, wHour, wMinute and wDay are more memorable than H, M and D. These rules apply to variables and other identifiers.

  • Identifiers can be up to 63 characters in length.
  • Identifiers must begin with either a letter or an underscore _ character.
  • Subsequent characters can be letters, underscores, or the digits 0-9.
  • Identifiers can't contain any other symbols, such as $, %, and *. Identifiers like Do@Start result in a syntax error.
  • Identifiers cannot use reserved words such as var, begin, end, and for, which have special meaning to the compiler.
  • Avoid using identifiers already defined in ESPL. For example, the words Boolean and Integer are predefined data types.
Giving a variable a type
When declaring a variable, you must state its type which defines the set of values the variable can have. In general, you declare a variable where you want to use it, such as local to a procedure or function. Precede all variable declarations with the reserved word var. The declaration itself consists of the variable's name, a colon, and the variable's type. Example:

var
  iValue: integer;
  iSum: integer;
  sLine: string;

Because iValue and iSum are both of type Integer, the variable declaration could look like this, with a comma separating the two variables of the same type:

var
  iValue, iSum: integer;
  sLine: string;

Variable Types
While there are more than four types of variables, this lesson will introduce the four that are most frequently used.


string

Strings are variable types used to represent groups of characters.  Every language has its own spin on how strings are stored and used.  ESPL has the best implementation for strings that I know of.  The compiler allocates memory as needed so you don't have to worry about allocating buffers or disposing of them as you do with C.  ESPL strings can be up to two gigabytes in length.  ESPL provides a rich library of functions for working with string variables.

integer
Integers are variable types used to store signed integers in the range -2,147,483,648 to 2,147,483,647.

real
Reals are variable types used to store floating point numbers in the range 2.9*10-39 to 1.7*10+38. Reals would be used whenever a value contains a decimal point.

boolean
Booleans are variable types used to store a boolean True or False. Booleans are used in evaluating logical expressions and conditional tests.

Initialization
Integer and real type variables will be initialized to zero. String and Boolean type variables do not have initial default values. They should be assigned initial values before they are used in expressions.

If you haven't used a compiled language before, you might wonder why it is necessary to declare identifiers before you use them.  The compiler must be given explicit information about the identifiers before the compiler can create executable code that uses them.  Also, because ESPL is a compiled language, your scripts will run much faster than applications you build with an interpreted language.  Declaring identifiers before you use them reduces the number of programming errors and increases the efficiency of the code.

Prior | Next