Apple II User Manual

Page of 257
    100 INPUT N
    110 IF N=INT(N) THEN 140
    120 PRINT "MUST BE INTEGER."
    130 GOTO 100
    140 RETURN
This program asks for two numbers (which must be integers), and then prints their sum.  The
subroutine in this program is lines 100 to 140.  The subroutine asks for a number, and if it is not
an integer, asks for a new number.  It will continue to ask until an integer value is typed in.
The main program prints "WHAT IS THE NUMBER," and then calls the subroutine so get the value
of the number into N.  When the subroutine returns (to line 40), the value input is saved in the
variable T.  This is done so that when the subroutine is called a second time, the value of the first
number will not be lost.
"SECOND NUMBER" is then printed, and the second value is entered when the subroutine is
again called.
When the subroutine returns the second time, "THE SUM IS" is printed, followed by the sum.
T contains the value of the first number that was entered and N contains the value of the second
number.
STOPPING A PROGRAM
The next statement in the program is a "STOP" statement.  This causes the program to stop
execution at line 90.  If the "STOP" statement was excluded from the program, we would "fall
into" the subroutine at line 100.  This is undesirable because we would be asked to input another
number.  If we did, the subroutine would try to return; and since there was no "GOSUB" which
called the subroutine, an RG error would occur.  Each "GOSUB" executed in a program should
have a matching "RETURN" executed later.  The opposite also applies:  a "RETURN" should be
encountered only if it is part of a subroutine which has been called by a "GOSUB."
Either "STOP" or "END" can be used to separate a program from its subroutines.  "STOP" will
print a message saying at what line the "STOP" was encountered.
211  ENTERING DATA
Suppose you had to enter numbers to your program that did not change each time the program was
run, but you would like it to be easy to change them if necessary.  BASIC contains special state-
ments, "READ" and "DATA," for this purpose.
Consider the following program:
     10 PRINT "GUESS A NUMBER";
     20 INPUT G
     30 READ D
     40 IF D = -999999 THEN 90
     50 IF D<>G THEN 30
     60 PRINT "YOU ARE CORRECT"
     70 END
     90 PRINT "BAD GUESS, TRY AGAIN."
     95 RESTORE
    100 GOTO 10
    110 DATA 1,393,-39,28,391,-8,0,3.14,90
    120 DATA 89,5,10,15,-34,-999999
When the "READ" statement is encountered, the effect is the same as an INPUT statement.  But,
instead of getting a number from the keyboard, a number is read from the "DATA" statements.
The first time a number is needed for a READ, the first number in the first DATA statement is
read.  The second time one is needed, the second number in the first DATA statement is read.
When the all numbers of the first DATA statement have been read in this manner, the second
DATA statement will be used.  DATA is always read sequentially in this manner, and there may
be any number of DATA statements in your program.
The purpose of this program is to play a little game in which you try to guess one of the numbers
contained in the DATA statements.  For each guess that is typed in, we read through all of the
numbers in the DATA statements until we find one that matches the guess.