Contents / Reference section / BASIC keywords / Previous letter / Next letter / Index


D

DATA

Purpose

DATA is used in conjunction with the keyword READ, and sometimes with RESTORE, to enable you to automatically make available any data (numbers and words) that will be needed by a program.

For example, if you were writing a geography quiz, you might want to use the names of 5 countries and their 5 capital cities each time you used the program. The names of the cities and countries can be entered as DATA in the program and will always be there when the program is run.

Computers using the language BASIC are really quite clumsy at handling information like this, as the demonstration program on the next page shows.

In the example program the DATA consists of lots of words. DATA statements can just as well contain numbers - or a mixture of words and numbers. In our example the words were all read into a string array.

It is essential that the DATA contains numbers where numeric variables are to be filled. Text information eg hello will just give 0.

There is no need to put each word in inverted commas unless leading spaces are important in the DATA words, for example

"    four spaces"

If you wish to have leading spaces then these words should be enclosed in inverted commas. Since a comma is used to separate items of DATA, if you want a comma in your DATA, you must enclose the DATA in inverted commas.

Examples

100 DATA "Allen Stephen",Stamp dealer, 01-246 8007, 24

130 DATA "TOP OF ROOF", 450, January

Description

A program object which must precede all lists of data intended for use by the READ statement.

Syntax

DATA <str-const>|<num-const>{,<str-const>|<num-const>}

Associated keywords

READ, RESTORE

Demonstration Program

10 REM geography quiz

20 DIM city$(5)

30 DIM country$(5)

40 FOR x=1 TO 5

50 READ city$(x)

60 READ country$(x)

70 NEXT x

80 right=0

110 FOR x=1 TO 10

120 r=RND(5)

130 PRINT "What city is the capital"

140 PRINT "of "; country$(r)

150 INPUT answer$

160 IF answer$=city$(r) THEN PROCyes ELSE PROCno

170 NEXT x

180 PRINT "You got ";right;

190 PRINT "correct out of 10"

200 END

500 DATA Paris, France, Reykjavik

505 DATA Iceland

510 DATA Moscow, Soviet Union

520 DATA Athens, Greece

530 DATA Spitzbergen, Spitzbergen

600 DEF PROCyes

610 PRINT "Well done!"

620 right=right+1

630 ENDPROC

700 DEF PROCno

710 PRINT "No, the capital of ";

720 PRINT country$(r);" is";city$(r)

730 ENDPROC

Line 10 is just a REMark which the computer ignores

Lines 20 and 30 tell the computer that we are going to use two string arrays - one to store the names of the 5 CITYs and the other to store the names of 5 countries. See page 120 for an explanation of arrays.

Line 40 sets up a FOR...NEXT loop that will go round 5 times.

Line 50 reads the next word (which will be a city) into the array city$ and then moves the "data pointer" on to point to the next word (which will be a country).

Line 60 reads the next piece of DATA into the country$ array.

Line 70 is the end marker of the FOR...NEXT loop.

Lines 110 to 170 loop 10 times through a 'question and answer' quiz.

Lines 500 to 530 contain the DATA used above.

Lines 600 to 630 is a procedure to deal with correct replies.

Lines 700 to 730 deal with incorrect replies.

DEF define

Purpose

The word DEF is used to inform the computer that a "procedure" or "function" is about to be defined. Once the computer has been informed that this procedure or function exists, then the procedure or function can be called by name anywhere in the program.

The definition of procedures and functions must not occur in the body of a program. They should be placed in a separate section which is not executed - for example after the final END in the program. This also aids readability.

The language BASIC has many predefined functions which the computer already knows about. For example, the function SQR enables it to calculate the square root of a number.

Often though, it is useful to be able to define your own functions. For example, you might want to have a function which calculates the VAT inclusive price of a product from the basic sale price by multiplying by 1.15.

A function always produces a result so you can write X=FN VAT. A procedure, on the other hand, is used to perform a number of actions, but it does not by itself produce a numerical result. For example, a procedure might be set up to clear the screen and draw a number of lines on the screen.

You may well feel confused, but do not be put off! The use of procedures and functions may be difficult to understand at first but it is well worth the effort. Their use greatly enhances the readability and reliability of programs.

The section below gives a more detailed explanation of the use of procedures and functions. It should be read in conjunction with the examples which follow.

Both procedures and functions may contain Local Variables which are declared using the word LOCAL. In the third example given below, K is declared as a local variable. This means that although K is used in this procedure its value is not defined when the procedure finishes. In fact the variable K might well be used elsewhere in the program. The variable K, elsewhere within the program, would not be altered by the use of the local variable K within the procedure. Any variable which is not declared as LOCAL will be available outside the procedure, in other words to the rest of the program.

Also both procedures and functions may have parameters passed to them. Look at the first example program below: line 1010 says

1010 DEF FNVAT(g)=1.15*g

"g" is called a "formal parameter" for the function FNVAT. It tells the computer that one number is going to be "passed" to the function when the function is used - and inside the function we have decided to use the letter g to represent the variable.

The procedure is "called" or used like this - for example

230 PRINT "VAT inclusive price ";

235 PRINT FNVAT(P)

and in this case "P" is the "actual parameter" for the function FNVAT. Whatever value "P" has will be used inside the procedure wherever reference is made to the "formal parameter" "g". This is very convenient since you can use any variable names that you like for the parameters inside the procedure. Then you can call the procedure with a quite different set of parameter names from the outside. Very often a procedure will be called from many different places in the program - and the actual parameters may have different names each time the procedure or function is called.

If a procedure or function is defined with (say) 3 formal parameters then, when it is called, 3 actual parameters must be supplied. See the fifth example below where three parameters are passed to the function.

The end of the procedure is indicated with the statement ENDPROC. The end of a multi-line function is indicated by a statement that starts with an = sign. The function is given the value of the expression to the right of the = sign.

Examples

First example - full program

210 REPEAT

220 INPUT "Basic price ",P

230 PRINT "VAT inclusive price ";

235 PRINT FNVAT(P)

240 UNTIL P=0

250 END

1000 REM one Line numeric function

1010 DEF FNVAT(g)=1.15*g

Second example - program section

Multi-line string function with one string parameter

1000 DEF FNREVERSE(A$)

1010 REM reverse the order of the letters in A$

1015 REM

1020 LOCAL d%,B$

1030 FOR d%=1 TO LEN(A$)

1040 B$=MID$(A$~d%~1)+B$

1050 NEXT d%

1060 =B$

Third example - program section

Multi-line procedure with 1 parameter

200 DEF PROCbye(X)

210 REM print bye X times

220 LOCAL K

230 FOR K=1 TO X

240 PRINT "bye"

250 NEXT K

260 ENDPROC

Fourth example - program section

This sets the background colour to a new value given in the parameter.

10 DEF PROCINITSCREEN(X)

20 REM clear screen and draw border

25 COLOUR 128+X

30 CLS

40 DRAW 1279,0 232

50 DRAW 1279 1023

60 DRAW 0,1023

70 DRAW 0,0

80 ENDPROC

Fifth example - full program

110 INPUT X,Y,Z

120 M=FNNEAN(X,Y,Z)

130 PRINT "The mean of ",X,Y,Z

140 PRINT "is ";M

150 END

8990 REM Single Line numeric function

8995 REM with three parameters

9000 DEF FNMEAN(A,B,C)=(A+B+C)/3

Description

A program object which must precede declaration of a user function or procedure. String and numeric functions and procedures may be defined. Multi-line functions and procedures are allowed. Procedures and functions need not be defined before they are called. All procedures and functions must be placed in the program where they will not be executed e.g. after the END statement.

Syntax

DEF FN|PROC<variable name>[(<string-var>| <num-var>{,<string-var>|<num-var>})]

Associated keywords

ENDPROC, FN, PROC
DEG degrees

Purpose

This function converts angles which are expressed in radians into degrees. 1 radian is equal to about 57 degrees.

Examples

100 X=DEG(PI/2)

300 angle=DEG (1.36)

PRINT DEG(PI/2)

Syntax

<num-var>=DEG<numeric>

Description

A function which converts radians to degrees.

Associated keywords

RAD, SIN, COS, TAN, ACS, ASN, ATN

DELETE

Purpose

The DELETE command is used to delete a group of lines from a program. It cannot be used as part of a program. You can specify which lines should be deleted with a command of the form

DELETE 120,340

This would remove everything between line 120 and line 340 inclusive.

To delete everything up to a certain line number (for example up to line 290) use DELETE 0,290.

To delete from line 500 to the last line, use as the "last line to be deleted" any number greater than the last line number in the program. Since the largest line number allowed is 32767,

DELETE 500,32767

will do the trick, but will take a long time.

To delete a single line just type the line number and press RETURN There is no need to use the DELETE command.

Examples

DELETE 0,540

DELETE 180,753

DELETE 540,32000

Syntax

DELETE <num-const>,<num-const>

Description

A command enabling a range of lines to be deleted from a program. Since DELETE is a command it cannot be used in a program or as part of a multiple statement line.

Associated keywords

LIST, OLD, NEW

DIM dimension of an array

Purpose

As well as simple numeric and string variables (such as "X" and "name$") it is possible to work with "arrays" of variables. These are extremely useful when working with groups of numbers or words. For example if one wanted to work with a set of information about the rooms in an hotel with 4 floors, each with 30 rooms, then an array of 4 by 30 entries can be created thus

DIM hotel(4,30)

Having set up an array, one can enter information into each of its "elements". For example the cost of the room per night might be £23.50

hotel(1,22) =23.50

hotel(4,1) =145. 00

In practice the statement DIM hotel(4,30) produces an array of 5 by 31 entries since the lowest array element is hotel(0,0).

All the above arrays are called "two dimension numeric arrays". Another array could contain the names of guests

DIM name$(4 30)

name$(1,22) ="Fred Smith"

name$(4,1) ="The Queen"

That sort of array is called a "two dimension string array".

Arrays may have one or more dimensions. A single dimension array would be appropriate for all the houses in a road. e.g.

DIM MainSt(150)

That sort of array is called a "single dimension numeric array". All arrays are normally dimensioned very early in the program. It is "illegal" to attempt to change the size of an array by re-dimensioning it later in the program. An array may have as many dimensions and as many elements in each dimension as the computer has space for - but you tend to run out of computer memory pretty fast with large arrays! It is essential that there is no space between the array name and the first bracket. Thus DIM A (10) is correct but DIMA (l0) will not define an array.

Examples

100 DIM partnumbers(1000)

3000 DIM empLoyeename$(35)

240 DIM ALL hours in the week(24,7)

100 DIM A(x)

Description

A statement which dimensions arrays. Arrays must be predeclared before use. After dimensioning all elements of arrays are initialised to zero for numeric arrays or null strings in the case of string arrays. The lowest element in an array is element zero. Thus DIM X(4) would create an array of 5 elements (0 to 4 inclusive).

There is a second and quite different use for the DIM statement. It can be used to reserve bytes in memory for special applications. To reserve 25 bytes, type

DIM X 24

Notice two things about this statement: firstly the space between the variable X and the (number of bytes minus 1) and secondly the absence of brackets around the "24". The address of the start of the group of 25 bytes is given in the variable X in this example.

Syntax

DIM <num-var>|<str-var>(<numeric>{,<numeric>})

DIM <num-var> <numeric>

Associated keywords

none

DIV division of whole numbers

Purpose

See page 299 which describes the word MOD for a full explanation. DIV is an operator which gives the whole number part of the result of a division. Thus

PRINT 11 DIV 4

gives 2 (leaving a "remainder" of 3).

Description

A binary operator performing integer division between its operands. The operands are converted to integers before division takes place.

Syntax

<num-var>=<numeric> DIV <numeric>

Associated keywords

MOD

DRAW

Purpose

This statement draws lines on the screen in MODEs 0,1,2,4 and 5. The DRAW statement is followed by two numbers which are the X and Y co-ordinates of the end of the line. The line starting point can either be the end of the last line that was drawn or else a new point if the MOVE statement has been used before the statement DRAW.

The screen is addressed as

1280 points wide X-axis, 0-1279

1024 points high Y-axis, 0-1023

regardless of the graphics mode selected. The origin (position 0,0) is normally at the bottom left of the screen.

The line drawn in the current graphics foreground colour. This can be changed using the GCOL statement.

Examples

780 DRAW X,Y

DRAW 135,200

Description

DRAW X,Y means draw a line to X, Y in the current foreground

colour.

DRAW X,Y is equivalent to PLOT 5,X,Y.

DRAW is one of a large group of line drawing statements. See PLOT on page 319 for others.

Syntax

DRAW <numeric>,<numeric>

Associated keywords

MODE, PLOT, MOVE, CLG, VDU, GCOL

Demonstration Program

140 MODE 5

160 REM Red background

170 GCOL 0, 129

175 CLG

180 REM Yellow foreground

190 GCOL 0,2

200 REM draw a box

210 MOVE 100,100

220 DRAW 400,100

230 DRAW 400,400

240 DRAW 100,400

250 DRAW 100,100

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001