QMBasic  -  Variable Names and Values

Top  Previous  Next

 

Variable names must commence with a letter and may contain letters, digits, periods (full stops), percentage signs and dollar signs. Names may also contain underscore characters but not as the last character of the name. Users are discouraged from defining names containing dollar signs for their own purposes as these are reserved to identify system functions and constants. Except as indicated elsewhere, there is no restriction on the length of a name though very long names may appear truncated in debugging information.

 

Although QMBasic imposes few restrictions on the choice of names, it is advisable to avoid using names which correspond to QMBasic statements, functions and keywords. The only reserved names which may not be usable in some contexts are

AND

GOSUB

ON

BEFORE

GOTO

OR

BY

GT

REPEAT

CAPTURING

IN

RETURNING

CAT

LE

SETTING

DO

LOCKED

STEP

ELSE

LT

THEN

EQ

MATCH

TO

FROM

MATCHES

TRAPPING

GE

NE

UNTIL

GO

NEXT

WHILE

 

 

QMBasic variables are type variant, that is, that they may hold, for example, an integer value at one point in time and a character string later on. The actual form in which the data is held is determined by how it was assigned. If a variable is set to contain a string of digits and is subsequently used in an arithmetic calculation, the value is converted internally to a numeric form without affecting the variable itself. If this arithmetic calculation was performed many times in a loop, it may be worth forcing a type conversion to prevent repeated temporary conversions. For this reason, QMBasic programs often contain apparently redundant looking statements of the form

 

A = A + 0        ;* Convert to numeric form

or

S = S : ""        ;* Convert to string form

 

Numeric values may are held as integers wherever possible, conversion to floating point format occurring when the result of an arithmetic operation is non-integer or when the value is too large to be stored as an integer.

 

A variable holding a string of no characters is referred to as a null string and is treated as a special case in many operations. Users familiar with SQL type environments should take care to distinguish the multivalue database meaning of the word null from its SQL meaning.

 

A string variable may hold any number of characters. The actual total limit for all strings in a program is imposed by the disk space available for paging and is typically many megabytes. Although QMBasic avoids copying strings unnecessarily whenever it can, operations involving very large strings are likely to have a detrimental effect on performance.

 

A variable may hold many other types of information. For example, a file variable holds a reference to an open file and is used in all statements that refer to that file. A subroutine variable contains a fast reference to a catalogued subroutine that has been loaded into memory. Users cannot directly create subroutine variables, they are the result of transforming a string variable holding the subroutine name when it is first called. Until otherwise determined, variables are initially unassigned. Reference to an unassigned variable (where no value has yet been stored) will cause a run time error.

 

 

Constants

 

Constant values may be numbers or strings.

 

Numeric constants are written as a sequence of digits, optionally preceded by a sign or containing a decimal point. If a sign is used, there must be no space between it and the first digit.

 

QMBasic also allows hexadecimal numbers in equated tokens and most expressions. These are written with a prefix of 0x as used in the C programming language (e.g. 0x23 is decimal 35).

 

 

String constants are sequences of characters enclosed by delimiters. Valid delimiter characters are the single quote ('), the double quote (") and the back slash (\). The delimiter at the start and end of the string value must be the same but there is no difference in the internal treatment of the delimiters.

 

The compiler imposes no limit on the length of a string literal value though it may not extend from one line to the next. Very long strings can be constructed by concatenating component substrings.

 

The mark characters are available as @FM, @VM, @SM, @TM and @IM. These are described in a later section.