Lesson 6

DATA, READ, GO SUB, RETURN, NOT

In the first five lessons we learned how to use 25 of the easiest and most common codewords in BASIC. Lessons 6-10 will explain another 25 BASIC codewords, which are only a little more difficult than the first 25.

DATA The word “data” means a type of information. Often the information is just numbers. The DATA codeword allows you to give the computer a list of items of data, for example a list of numbers.

READ The READ codeword tells the computer to read all the items in a list of data.

GO SUB This codeword is a bit like GOTO. It tells the computer to go to a line in the program where you have started something we call a “subroutine”. So GO SUB means “GO to SUBroutine”.

What is a subroutine? It is a separate section of the code which is used several times in the program. We use subroutines so that we don’t have to write the same lines of code several times.

RETURN When we write  subroutine we put a RETURN codeword at the end of the section of code. This tells the computer to returun (go back) to where it was in the program when it jumped to the subroutine.

NOT This codeword tells the computer to test if a condition is not true.

The DATA codeword

The DATA codeword is a statement.

The DATA codeword provides a list of items of data for a program. These items may be numbers, which are separated by commas, and which the computer will use as values. Or they might be sequences of letters, numbers and other characters – we call these sequences “strings”. A program might tell the computer to display a string on the screen.

How to use the DATA codeword

A simple example of using the DATA codeword is this.

50 DATA 31

This line of the program sets up the number 31 ready for the computer to do something with it. But first the computer will need to read in the data and assign it the value 31 to a variable that can then be used in the program. The READ codeword is the next one we learn about in this lesson.

If you want to set up a list of numbers for the computer to use in your program, you must separated them by commas, like this:

51 DATA 14, 3, 79

You can also use the DATA codeword to set up a string of numbers, letters and other characters. For example:

52 DATA "This is my birthday"

Line 52 provides the computer with just one data string.

Here is another example:

53 DATA "Date", "Month", "Year"

Line 53 provides the computer with three data strings.

Once some data has been provided in the program, you can tell the computer to look at that data with a READ codeword, as explained next.

The READ codeword

The READ codeword is a statement/command.

The READ codeword is used in conjunction with DATA to assign the values provided by DATA to variables.

How to use the READ codeword

After your program has provided the computer with an item of data, you can tell the computer to look at and use that data item with the READ codeword. If your program has provided the computer with an item of data in a program line like this:

54 DATA 31

you can tell the computer to look at and use that data with the READ codeword, like this:

55 READ date

Line 55 tells the computer to look at (in other words, to read) the next item of data it has been given, and to assign its value to the variable date. So if the program has these two lines one after the other:

54 DATA 31
55 READ date

the computer will know that it is being provided with the item of data, 31, and that it must read this data and assign its value to the variable  date.

Using the READ codeword for strings

In just the same way that a number value can be assigned to a variable by using READ, you can tell the computer to assign a string to a variable in the same way. Look at these lines of program:

56 DATA "This is my birthday"
57 READ b
58 PRINT b

Here line 56 tells the computer that some data is being provided the string This is my birthday.

Then line 57 tells the computer to assign that string to the variable  birthdaymessage. And line 58 tells the computer to display whatever has been assigned to birthdaymessage, which is the string  This is my birthday, so the computer will display  This is my birthday on the screen.

Using the READ codeword for more than one item of data

We learned in the previous section that when two or more items of data are provided by a DATA codeword they must be separated by commas. The same is true if you want to use the READ codeword to tell the computer to look at and use two or more items of data. For example, the line:

59 DATA 14, 3, 79

tells the computer that it is being provided with three numbers. So if you want the computer to read all three numbers and assign them to three variables, you might write the next ine of the program like this:

60 READ date, month, year

This line tells the computer to read the next item of data (14) and assign its value to the variable date; then to read the following item of data (3) and assign its value to the variable month; and then to read the third item of data in line 59 (which is 79), and assign its value to the variable year.

So if your program has these two lines one after the other:

61 DATA 14, 3, 79
62 READ date, month, year

when the computer has obeyed the instructions in both lines the variable date will have the value 14, the variable month will have the value 3, and the variable year will have the value 79.

The GO SUB codeword

The GO SUB codeword is a statement/command.

The GO SUB codeword tells the computer to jump to what we call a “subroutine” in the program (GO TO SUBroutine). A subroutine is a separate section of the program – usually a section which is needed a few times or more in the program. The idea of having a subroutine is to save you having to write the same lines of program code in different parts of your program.

How to use the GO SUB codeword

GO SUB is very easy to use.  You can simply put a number after it. When the computer reaches the program line in which you have put the GO SUB codeword, the program tells the computer to jump to that line number. For example:

70 GO SUB 1000

tells the computer to jump to line 1000 where it will find the start of a subroutine. You can make a subroutine as short or as long as you wish. It could be just a few lines, or it could be dozens of lines. Whenever you use GO SUB to tell the computer to jump to that subroutine, the computer will carry out the instructions in the subroutine.

It is important to remember when you write the lines of code for a subroutine that you must end the subroutine with a RETURN codeword, which we explain next.

The RETURN codeword

The RETURN codeword is a statement/command.

You must use RETURN to end a subroutine. Then the computer will know that it should jump back in your program to the line after the one where you used GO SUB to tell it to jump to the start of the subroutine.

How to use the RETURN codeword

RETURN is very simple to use. It goes on the last line of your subroutine. Simply put the line number followed by RETURN, for example:

The NOT codeword

The NOT codeword is a logical operator.

How to use the NOT codeword

The NOT codeword tells the computer to test if a condition is not true, in other words – if the condition is false.

Here is a simple IF statement which uses a NOT condition:

81 IF NOT cats = 2 THEN PRINT "I do not have 2 cats"

This line of the program tells the computer that if the value of the variable cats is not 2 then it should display the message I do not have 2 cats.

You can also use the NOT codeword together with IF and THEN to tell the computer to test whether the value of a variable is 0. Here is an example of how you can do this:

91 IF NOT dogs THEN PRINT "I do not have any dogs."

Remember that the test IF dogs is a test to see if the value of dogs is not 0. This condition is true if the value of dogs is not 0. So the condition is false if the value of dogs is 0.

So line 91 of the program tells the computer that if the value of dogs is zero it should print the message I do not have any dogs. But if the value of dogs is not 0 then the computer does nothing special – it just continues on to the next line of the program.

Example Program

This program draws a calendar for a month entered by the user. You could use it to display the month you were born. Lines 850 to 880 asks for the year and month to display. Lines 900 to 990 works out the name of the month and how many days it has.Line 1000 to 1030 calculates which day of the week the day starts on In lines 1040 to 1080 we draw the calendar.

 850 CLS
 860   PRINT AT 1,10;"Calendar"
 861   REM Display a title
 870   INPUT "Enter a Year after 1752 ";y: IF y 871 REM Ask for the year that the user wants to know about and check it's after 1752
 880   INPUT "Enter a Month (1-12) ";m: IF m12 THEN GO TO 880
 881   REM Ask for a month to display and check it is between 1 and 12
 890   LET s=0
 900   FOR n=1 TO m-1
 901   REM Now read through our data to find how many days there were in that year before the user's chosen month
 910   READ m$: READ days
 920   LET s=s+days
 925   REM s is the number of days since the start of the year
 930   NEXT n
 940   READ m$
 941   REM Now read the name of the month
 945   READ days
 946   REM and read the number of days in this month
 950   PRINT AT 4,8;m$: PRINT AT 4,18;y
 955   REM display the month and year
 960   PRINT AT 7,2;"Mon Tue Wed Thu Fri Sat Sun"
 965   REM Display the header for the days of the week
 970   IF NOT (y=4*INT (y/4) AND (y<>100*INT (y/100) OR y=400*INT (y/400))) THEN GO TO 1000
 971   REM Check if this year is a leap year
 980   IF m=2 THEN LET days=29
 981   REM if it is and our month is February it has 29 days
 990   IF m>2 THEN LET s=s+1
 991   REM If the month comes after February we add a day because February had 29 days
 1000   LET d=5+y+INT ((y-1)/4)-INT ((y-1)/100)+INT ((y-1)/400)
 1001   REM Calculate which day of the week the year starts
 1010   LET d=d+s
 1011   REM Then add on the number of days so far in the year that we calculated above
 1020   LET y=9
 1021   REM Start drawing the dates just below the days of the week
 1030   LET x=3+4*(1+d-7*INT ((1+d)/7))
 1031   REM Calculate where the first day starts on the screen
 1040   FOR a=1 TO days
 1041   REM We now display the number of days for the month we are interested in
 1050   PRINT AT y,x;a
 1060   LET x=x+4
 1062   REM Move the screen position along by 4 spaces because the days of the week are 4 spaces apart
 1070   IF x>28 THEN LET x=3: LET y=y+1
 1071   REM If the screen position has gone past the end of the week, reset it to the start and move down the screen
 1080   NEXT a
 1081   REM Continue drawing until we have displayed all the days
 1090   DATA "January",31,"February",28,"March",31
 1100   DATA "April",30,"May",31,"June",30
 1110   DATA "July",31,"August",31,"September",30
 1120   DATA "October",31,"November",30,"December",31
 1121   REM This is our data for the names of the months and how many days are in each month