Types and Variables in BASIC

Data storage using built-in and user defined structures

© Guy Lecky-Thompson

Article covering the use of data types and variables in BBC BASIC programming, including integers, arrays, strings, local and static variables, as well as structures.

Introduction

This article is part of the Learn to Program with BBC BASIC series, and covers the use of variables in BBC BASIC programming. It is useful prequel to reading about loops, condition testing, and other topics, as well as introducing some vital concepts in computer programming in general.

Before reading the entire article, the Using Variables in Programming article gives some good background information that is language-neutral. It would be of benefit to those who have not programmed before, and have not met the concept of types and variables previously.

To briefly recap : variables are places that a computer programmer can store information, either temporarily or semi-permanently. That is, the longest time that the variable will contain the information is the life of the program.

Variables can be seen as letter boxes into which information can be placed, retrieved, changed, and manipulated. Each box can only contain information that it has been designed for, and as such a system of typing exists whereby a variable is declared to be capable of containing a certain type of information:

This article will cover the above from the BASIC point of view, and in particular the BBC BASIC language.

Variable Naming

Although BBC BASIC is a strongly typed language, there is no formal declaration of variables. They are created as they are accessed, through direct or indirect assignment, or as a formal or actual parameter passed to a function or procedure.

Variable names may start with a letter or underscore, but can otherwise contain any letter or number, and the underscore character. They are also case sensitive, and the final character gives the type (where specified). Although we noted that BBC BASIC is strongly typed, this only applies where the type has been specified.

It is good practice to use variable names which are long enough to describe the function that they fulfil, short enough to remember, and follow a naming convention (with underscores, etc.) that is consistent though the program.

Types

BBC BASIC has several numeric types:

Integer variables are denoted by the presence of a % sign at the end, and bytes by the presence of a & sign. Should the type not be specified then it is derived at runtime. Thus, the following are all allowed:

realNumber = 5
integerNumber = 5.5
newReal = 5.5
newInteger% = 5

The above illustrates the importance of using strong typing, where available, and intelligent variable names where not. It is up to the programmer to decide exactly which mix to follow.

BBC BASIC also has a string type, denoted by a $ character at the end of the variable name. Implementations usually require that strings are strongly typed, thus the following would not be allowed:

myString = "This is not allowed"

However, the correct form myString$ would be accepted.

Static Variables

In the BBC BASIC language, the A% to Z% variable names are reserved for so-called 'static' variables. These are accessed more quickly than other variables, and also hold their value even when the program is halted, and another started. They are therefore persistent, which has some advantages and disadvantages.

The most important point to remember is that they need to be explicitly cleared when control of a program is transferred to another. However, when writing a suite of related applications, it can be useful to rely on the fact that, in sequential operation, the values will be held and can be used to pass information down the chain.

Arrays

Any string or number can be used as the basis for an array. An array is a collection of boxes into which values can be stored, but where they are referred to by one name, together with an index. If this seems a little abstract, then think of a text as an array of words - each one has a number which refers to its place in the text:

DIM myText$(1499) : REM 1500 words of text

In the above example, the array can contain 0 - 1499 words. We say that the index is zero based. All arrays must be dimensioned before they can be used, and they are strongly typed, with no mixing within an array.

Arrays can be multi-dimensional:

DIM pixelColors&(639, 479) : REM A 640x480 screen

Stack space and sizing can become an issue on occasion - the above example consumes about 300kb of memory, and is probably not the best way to store this kind of information.

Structures

User defined data structures are a very useful way to group data under a single name. If we imagine a collection of address cards, each one containing a name, address, and telephone number, and then imagine how these might be stored in a program without using structures, we quickly run into issues. For example:

DIM szName$(100)
DIM nHouseNumber%(100)
DIM szStreet$(100)
DIM szCity$(100)
DIM szTelephoneNumber(100)

Now, if we want to store more addresses, we must resize all the DIM statements. Not only that, but if we wish to access a single record, we need to refer to five or more variables. It is much more convenient to break these down into user-defined objects:

DIM sAddress{szName$, sAddress{nHouseNumber%, szStreet$, szCity$}, szPhone$}

A bonus of using user defined structures is that we can also mix data types, as can be seen in the above example. To store 101 address cards, we can just dimension an array:

DIM oAddressBook{(100)} = sAddress{}

If this looks a little odd, then it is because, unlike C (for example), there are no provisions for type keywords (int, char **, etc.) We access each object using dotted notation:

oAddressBook(0).szName$ = "My Name"
oAddressBook(0).sAddress.nHouseNumber% = 42

Of course, if we had not declared an array, a single sAddress structure can be accessed though the dotted notation in a similar way to the above.

Links


The copyright of the article Types and Variables in BASIC in Computer Programming Tutorials is owned by Guy Lecky-Thompson. Permission to republish Types and Variables in BASIC must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo