Variables And Their Types

Top  Previous  Next

Variables are used to store values during the execution of an application. Each variable has a name (the identifier used to refer to the value the variable contains) and a type, which specifies how much data this variable can contain, and also what kind of data it may contain.

 

note_warning-wt

Not every variable type is supported on every platform. You will find related information in the "Supported Variable Types" topic in the platform documentation (for example, EM1000's is here). If you attempt to use a type which is not supported by your platform you will most probably get "platform does not export XXX function" error during compilation.

 

Variables are defined using the Dim Statement prior to being used in code. The simplest syntax for defining a variable would be something like:

 

 

dim x as integer ' x is a variable of type 'integer'.

dim str as string(32) ' str is a variable of type 'string' with a maximum length of 32 characters (bytes).

 

 

A variable name must begin with a letter, and must be unique within the same scope, which is the range from which the variable can be referenced.

When you define a variable, some space is reserved for it in memory; later, while the program executes, this memory space can hold a value.

Variables are assigned values like this:

 

 

x = 15 ' x is now 15.

x = y ' x is now equal to y.

str = "foobar" ' str now contains the string 'foobar' (with no quotes).

 

Types of Variables

Tibbo BASIC supports the following variable types:

Name

Description

byte

Hardware-level. Unsigned. Takes 1 byte in memory. Can hold integer numerical values from 0 to 255 (&hFF).

word

Hardware-level. Unsigned. Takes 2 bytes in memory. Can hold integer numerical values from 0 to 65535 (&hFFFF).

dword

Hardware-level. Unsigned. Takes 4 bytes in memory. Can hold integer numerical values from 0 to 4294967295 (&hFFFFFFFF).

char

Hardware-level. Signed. Takes 1 byte in memory. Can hold integer numerical values from -128 to 127.

short

Hardware-level. Signed. Takes 2 bytes in memory. Can hold integer numerical values from -32768 to 32767.

integer

Compiler-level. Synonym for short; substituted for short at compilation. Exists for compatibility with other BASIC implementations.

long

Hardware-level. Signed. Takes 4 bytes in memory. Can hold integer numerical values from -2147483648 to 2147483647

real

Hardware-level. Signed, in standard "IEEE" floating-point format. Can hold integer and fractional numerical values from +/- 1.175494E-38 to +/- 3.402823E+38. Real calculations are intrinsically imprecise. Result of floating-point calculations may also differ slightly on different computing platforms. Additionally, floating-point calculations can lead to floating-point errors: #INF, -#INF, #NaN. In the debug mode, any such error causes an FPERR exception.

float

Compiler-level. Synonym for real; substituted for real at compilation. Exists for compatibility with other BASIC implementations.

string

Hardware-level. Takes up to 257 bytes in memory (max string size can be defined separately for each string variable). Strings can actually be up to 255 bytes long but always begin with a 2-byte header -- 1 byte specifies current length, and 1 byte specifies maximum length. Each character is encoded using an ASCII (single-byte) code.

boolean

Compiler-level. Intended to contain one of two possible values (true or false). Substituted for byte at compilation.

user-defined structures

Each user-defined structure can include several member variables of different types. More about structures here.

user-defined enumeration types

Compiler-level. These are additional, user-defined, data types. More about these under User-Defined Types.

note_further-wt

Hardware-level types are actually implemented on the machine which is used to run the final program produced by the compiler.

Compiler-level types are substituted by other variable types on compile-time. The actual machine uses other variable types to represent them; they are implemented for convenience while programming.