QMBasic - Variable names and values

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.


Except when using the $MODE EXPLICIT compiler directive, variables do not need to be declared. The compiler adds variable names to its internal symbol table when they are first encountered. Use of explicit declaration allows misspelled variable names to be trapped by the compiler but can be inconvenient. When this mode is enabled, variables must be declared using the DIMENSION statement.


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







































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 is performed many times in a loop, it may be worth forcing a type conversion to prevent repeated temporary conversions. For this reason, multivalue Basic programs sometimes contain apparently redundant looking statements of the form


A = A + 0        ;* Convert to numeric form


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


QMBasic provides a better way to do this by use of the INT(), NUMERIC(), STR() and BOOL() functions.


Numeric values 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 typically far exceeds application needs. Although QMBasic avoids copying strings unnecessarily whenever it can, operations involving very large strings are likely to have a detrimental effect on performance.


A Boolean variable holds a True or False value. In general, these are interchangeable with numeric values 1 and 0 respectively. Support for the internal Boolean data type was added in release 3.2-2. When Boolean values are stored in dynamic arrays, the numeric representation is used.


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 as an aid to program debugging. This can be changed to become a warning message by use of the UNASS.WARNING mode of the OPTION command.





Constant values may be numbers, strings or Boolean values.


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. Regardless of the setting of the national language support decimal separator character (see SETNLS), the decimal separator in a QMBasic numeric constant is always the period.


If the NUMERIC.EXPONENT mode of the OPTION command is enabled, conversion of a character string to numeric form will accept exponent style values such as 123.45E-1.


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 backslash (\). 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. On ECS mode systems, string constants may contain characters from the extended character set.


The compiler imposes no limit on the length of a string literal value though it may not extend from one line to the next. To improve program readability, 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.



The Boolean constants are written as @TRUE or @FALSE. Prior to release 3.3-0, these evaluated to their numeric equivalents, 1 and 0 respectively. Release 3.3-0 onwards evaluate these as the internal Boolean data type, however, the NUMERIC.BOOLEAN option of the $MODE compiler directive can be used to revert to the old behaviour. This should only be used if programs must be able to run on releases prior to 3.2-2 that did not support the Boolean data type.