What we need is a little program to demonstrate this. If you have anything in the editor, get rid of it by pressing New. You will be prompted if you have not saved your previous masterpiece, so save it or discard it as you wish. Now type:
Run it, just to see it works. Save it because we'll be using it a lot later. Anybody who ever went to school once should understand the principles at work here, it's a bit like algebra. If you change the second line for different values of Radius and re-run the program, it will give you a different result each time. What is happening? When the program runs, it gets to the second line and sees we are trying to assign a value of 5 to something called Radius. BASIC maintains a list of memory locations which it creates afresh every time a program is run. As this is the first active line, it won't have a memory location called Radius, so one is very nicely made for us. Having done this, the value of 5 is written into the new location.
REM Area of a circle Radius=5 Area=3.14159*Radius*Radius PRINT "The area of your circle is ";Area END
On the next line, something similar happens with the value for Area. The assignment is an expression, however, so it needs to calculate this first. When BASIC gets to the section with Radius, it goes to its variable list and finds the value. This is 5 from the previous line, so 5 is substituted into the expression. Once complete, the result is assigned to the newly created variable Area. The fourth line has BASIC retrieve the value and print it out after dressing with some text for clarity.
If you have opened any of the programs that come with BBC BASIC, you will have seen lots of lines like the above, so these things called variables must be quite important. We'll dwell on them for a while. Variables by default are floating point numbers, i.e. ones with a decimal place, but there are others. BBC BASIC supports four different types. These are defined by the last character of the variable's name:
Type | Description | Examples | Range | Character |
---|---|---|---|---|
Floating point | Decimals | 3.142, 0.001, 1E10-2 | –5.9E-39 to 3.4E38 | # or none |
Integer | Whole numbers | 32000, 4, -66 | –2147483648 to +2147483647 | % |
Byte | Whole numbers | 0, 128, 255 | 0 to 255 | & |
String | Text | "BBC BASIC", "Hello" | any number of characters from 0 to 65535 | $ |
A variable name can start with any letter of the alphabet (upper or lower case), an underscore ( _ ) or a grave accent ( ` ). It can then have any combination of upper and lowercase letters, numbers or underscores. The length of the name is not limited, but usually names are kept shortish to save typing. Finally there is the character that gives the type, from the table above. If no character is given, floating point is assumed.
Names are case sensitive so AREA, area and ArEa are different as far as BBC BASIC is concerned (not necessarily so with other dialects, but once you've used this one, you won't need any other). Also BBC BASIC is quite happy with variables of different types with the same name, so Area%, Area and Area$ would not confuse it, but might confuse you. It's your choice: be careful!
A final limitation is that variable names must not start with a BASIC command or keyword like PRINT, TAB or CLS. So, these are valid names:
... and these are not:
_my_float AnInteger% FirstName$ NUM1
Naming variables is an art in itself. Once everything was restricted to one or two letter names which tended to render a program an unintelligible mess. Not so now. BBC BASIC will recognise variable names of (practically) any length. These two would be considered different:
1_man_went_to_mow (starts with a number) Went to mow a meadow (contains spaces) PRINT_TOTAL (starts with a keyword)
However you would soon get fed up of typing those in every time you needed them and there is a hit on the performance speed of your program with lots of long names. Ten to fifteen letters is a good compromise. Shorter if possible, but get some meaning in there so readers can tell what the data represents. There are two conventions, one puts underscores between separate words to make them easier to read as spaces are not allowed. The other capitalizes the first letter of each word, like so:
averyveryveryveryveryverylongname1 averyveryveryveryveryverylongname2
I tend to use the second method, but it's up to you.
number_of_times NumberOfTimes
As we have seen, variables are created the first time BASIC encounters them in an assignment statement. They must be on the left of the equals sign. An undeclared variable on the right will lead to an error. To try it, change the third line to:
and run. Always be careful of case!
Area=3.14159*Radius*radius
Correct our little bug above and give the circle program a run through. Instead of closing the window, type:
The variables are still in memory and are only cleared when the output window is closed or the program is run again. Type:
PRINT Radius PRINT Area
Now we get an error. CLEAR forces BASIC to wipe the slate clean and forget all its variables. It can be used in programs to re-initialize before doing another run.
CLEAR PRINT Radius
Tip: Running your program from
the output window |
||
Just to go slightly off-topic for a second, whilst
we're talking about
using the output window, you can get your program to run again without
closing the window. At the cursor type:and off it goes. It is slightly quicker than closing the window and waiting for it to open again. |
Static Variables
BBC BASIC pre-declares 26 integer variables called A% through to Z%. Their properties are the same as other integer variables, but they have one special use. They are not cleared when the program is rerun, even if the program calls the CLEAR command mentioned above. They are not even cleared if the program calls another one. When BBC BASIC is closed, of course, all values are lost. Use of this functionality probably falls into the advanced category but it is worth noting.
Exercises
Now we have a good idea of variables and their use, try the following exercises:
1) Modify the area program to give the circumference of a circle
3.14159*diameter.
2) Print the area of a rectangle, when given the width and height.
3) Assign a string variable to contain your name and output:
where xxxx is, of course, your name.
Hello, xxxx
CONTENTS |
CHAPTER 5 |