SuperBasic Data Types

Publication: Update Magazine
Pub Details: Update October 1993
Date: October 1993
Pages: 38

Whenever we use variables in a SuperBasic program, we tell the SuperBasic interpreter what type of data it may hold in each variable. We don’t necessarily do this deliberately as the system ‘defaults’ to a particular type. But what is a type and how do we take advantage of them?

It’s all to do with data representation; if we’re dealing with numbers in an accounting program, we’re likely to want to use fairly large numbers which may optionally include a decimal point. If we were writing a program to look after the stock levels in our factory, we’d probably want to use whole numbers, and if we’re writing an address book program, we want to deal with strings of characters. These are the three fundamental data types accepted and used by the SuperBasic interpreter.

The first type, numbers which may include decimal points and can take on extremely large values, is known as floating point. On the QL, floating point numbers have a range of in the region 10 to the power of ± 615.

The second data type, that comprising whole numbers, is known as the integer data type. On the QL, integers can be any whole number between 32768 and -32767

The last type, comprising of strings of characters, is known as the string data type, and strings can be from zero characters long (known as the null string ie, “”) up as far as 32766 (NOT 32767!) characters long.

The reason for the existence of the three types is that different applications demand different kinds of data, and having to hold all data as one particular type would be very inefficient. QL SuperBasic variables are specified in terms of type by following the name of the variable with a special symbol. If there is no symbol, as in

variable

then the data type is floating point. As it is implied rather than specified, this is the type adopted by default.

Integer variables are introduced by following the name with a per cent sign, as in

variable%

and string variables are created by terminating the name in a dollar sign:

variable$

The SuperBasic interpreter exercises something known as type coercion, which means converting data from one type into another. For example, we may have a variable, v, which holds the number 23. As v has no special symbol at the end, it is a floating point variable. This means that its value, 23, is held as a floating point number inside the QL. Nevertheless, we can put the value into an integer variable

x%=v

because the value of v (23) also happens to be a valid value for an integer. The SuperBasic interpreter coerces the floating point representation of 23 into its integer representation before the value is placed into x%. This coercion is often very useful, as we don’t need to check for operations such as the assignment above, and old BASIC functions such as STR$ and VAL are done away with. To see whether a coercion could be successful, we need only to consider the generality of each type. As a string variable can hold an arbitrary string of characters, and as all numbers may be written down as characters (and therefore stored as characters), it follows that a string variable can always be assigned a numeric value. The statement

v$=1234.56

will put the seven characters “1”, “2”, “3”, “4”, “.”, “5” and “6” into the string variable v$. The value of 1234.56 (ie, the number 1234.56) will not be stored, simply its string representation. The string data type is therefore more general than numeric types such as floating point and integers.

Floating point is more general than integer, because every integer may also be represented as a floating point number. This makes the integer data type the least general and string type the most general.

There is a further data type accepted by the SuperBasic interpreter, called the name data type. This is used for procedure, variable and function names, and for file and device names. It is comprised of a string of characters, just like the string data type, but is slightly less general than string as not all strings are valid names. It is used whenever you specify a filename, as in

OPEN#3,mdv1_myfile

As you can see, the only difference between it and a normal string is that it is not surrounded in quote marks. It is perfectly permissable to add the quote marks, and thus turn it into a string, but it is generally less convenient to do so. The name data type is a sub-branch or leaf of the string data type.

UN-TYPED DATA

Occasionally you will come across instances of data which don’t seem to have a type specified at all. Although you may think that this makes it default to the floating point type, this isn’t quite true. Let’s take a typical defined function:

1000 DEFine FuNction MYFUNC%(a,b)
1010 LOCal c,d$
1020 c=a INSTR b
1030 IF c=0 THEN d$='' ELSE d$='OKAY'
1040 RETurn LEN(d$)
1050 END DEFine

If we make a list of all the identifiers (names) in this function, we see MYFUNC%, a, b, c, d$. The function itself, MYFUNC%, is terminated in a % sign, which indicates that the function is typed. It returns an integer result. Likewise, the two local variables c and d$ are typed, c is a floating point variable and d$ is a string variable. What about the two formal parameters, a and b? No type is specified for them, yet they are used as string variables within the function.

This is another rule of the language followed by the SuperBasic interpreter: formal parameters to functions and procedures are un-typed, and the actual type is derived from the usage of the variables within the procedure or function. As we’re using them as strings here, it doesn’t matter what type the actual parameters are as string is the most general type. If we had used them as integers in the function, we’d get an ‘Error in expression’ error (or possibly ‘Overflow’) if the actual values of ‘a’ and ‘b’ could not be coerced into integers.

There is nothing to stop us specifying the types of the formal parameters within the function, as in

1000 DEFine FuNction MYFUNC%(a$,b$)

but as the interpreter still follows its own rules, the type we specify will be over-ridden by the type dictated by the usage.

The reason for having un-typed formal parameters is a little obscure, but as it makes little difference to the way we write programs it doesn’t really matter. Some of SuperBasic’s rules for typing DO affect us, though, and if we don’t know about them it is very easy to get confused. The main point to watch is the ‘flow of control’ statements

SELect
REPeat
FOR

Each of these has a ‘control variable’ (or, more accurately, a control identifier) which is specified in the construct:

SELect ON var
REPeat loop_1
FOR x=0 TO 10

The important thing about these control variables is that in all three cases they MUST be floating point variables. We cannot have

SELect on var%

or

REPeat loop$

The annoying thing is that if we enter such an erroneous line into a program, the SuperBasic interpreter won’t complain. Things will go wrong as soon as we run it, though, and the particular error generated may be extremely obscure. So watch out!