|
|
xxxxxxxxxxxxxxxxxx |
BASIC LESSON 8
| |||||||||||||
| INPUT "How many numbers do you want to average "; N | |
| REDIM A(N) | |
| FOR I = 1 TO N | |
| INPUT "Enter your next number : ", A(I) | |
| NEXT I | |
If the user repeats the program, he might have another list of a different length. An ordinary DIM statement would produce an error message in that situation.
When you redimension an array, all the entries in that array are reset to 0.
| N! = 1 · 2 · 3 · 4 · … · N . |
The function name is "fact", and it depends on one parameter N.
| FUNCTION fact(N) | ||
| P = 1 | ||
| FOR I = 1 TO N | ||
| P = P * I | ||
| NEXT I | ||
| fact = P | ||
| END FUNCTION | ||
If later in the program you insert say the statement Y = fact (5), then Basic will assign Y the value 5! = 120. If the statement reads Y = fact (M), then Basic assigns Y the value M! using the present value of M.
The general format of a function subprogram is
| FUNCTION name (A, B, C,...) |
| instructions |
| name = expression |
| END FUNCTION |
In place of "name" you insert some name for your function, and instead of A, B, C, …, you insert the variables upon which your function depends. The instructions will involve some calculations that Basic must perform to compute the value of the function.
When you begin to type a function subprogram and enter the first line naming the function, Basic jumps to its special subprogram window. In this window will already appear the name of the function as well as the final END FUNCTION statement - in between these you are to fill in the instructions to Basic for computing the function. After you type the function subprogram in this window, you can return to the main program by clicking "View - Subs" and then double - clicking the name of your main program. You can switch back and forth between your main program and your subprograms by clicking "View - Subs" and choosing the program you want to edit.
When you print a Basic program having subprograms, the main program prints first, and then all the subprograms in alphabetical order according to subprogram names.
When you save a program containing a function subprogram, Basic inserts a DECLARE FUNCTION statement at the beginning of the main program. The DECLARE FUNCTION statement accompanying the above subprogram for factorials is
| DECLARE FUNCTION fact! (N!) | . |
Note that the DECLARE statement includes symbols indicating the types of the input parameters and the function type. If you do not designate these yourself when you type the first line of the function subprogram, Basic will by default make everything single precision, as denoted by the exclamation point "!".
Following is a function subprogram, named "volcyl", for computing the volume of a cylinder of radius R and height H; the function and all variables are double precision:
| FUNCTION volcyl# (R#, H#) | |
| volcyl = 3.1415926536# * R# * R# * H# | |
| END FUNCTION | |
If you later plug values into this function, say with the statement
| Z = volcyl (X, Y) , |
then X and Y must already have been designated as double precision variables; if X and Y are of a different type, such as integer or only single precision, you will get a "type-mismatch" error message. The DECLARE FUNCTION statement for the above program reads
| DECLARE FUNCTION volcyl# (R#, H#) . |
If you prefer to begin your Basic programs with REM statements, it can be annoying when Basic inserts DECLARE statements preceding your already typed REM statements. To get the REM statements back to the beginning, you can either
1) manually move all your REM statements to the beginning with copy and paste methods,A function subprogram can list also an array as one of its variables. (The DEF fn construction does not allow this feature.) Here is a function subprogram for computing the average in an array A of numbers indexed from 1 to N:or (perhaps the easier way),
2) wait until your program is completely written and saved before typing your REM statements at the beginning.
| FUNCTION avg (N, A()) | ||
| sum = 0 | ||
| FOR I = 1 TO N | ||
| sum = sum + A(I) | ||
| NEXT I | ||
| avg = sum / N | ||
| END FUNCTION | ||
The parentheses in A() signify that A is an array. The number N is included as a variable of the function because its value is necessary for computing the average.
Recall that a polynomial p of degree n has the form
| p(x) = a0 + a1x + a2x2 + a3x3 + ··· + anxn . |
To evaluate this polynomial, Basic needs to know the degree n, the array of coefficients a0, a1, ··· , an, and the x-value of interest. The function subprogram below computes p(x):
| FUNCTION poly (N, A(), X) | ||
| sum = A(0) | ||
| FOR I = 1 TO N | ||
| sum = sum + A(I) * X ^ I | ||
| NEXT I | ||
| poly = sum | ||
| END FUNCTION | ||
This subprogram works fine, but it is somewhat inefficient because at every step in the FOR - NEXT loop Basic must calculate X ^ I from scratch. We can save Basic some work (and computer run time) if at each step we use the calculation of X ^ I from the previous step. The following program illustrates a more efficient algorithm:
| FUNCTION poly (N, A(), X) | ||
| sum = A(0) | ||
| Xpower = 1 | ||
| FOR I = 1 TO N | ||
| Xpower = Xpower * X | ||
| sum = sum + A(I) * Xpower | ||
| NEXT I | ||
| poly = sum | ||
| END FUNCTION | ||
Here are statements of a main program, prompting for the degree and coefficients of a polynomial p(x) and an x-value, and using the above subprogram to evaluate p(x) :
| INPUT "What is the degree of your polynomial "; N | |
| DIM A(0 TO N) | |
| FOR I = 0 TO N | |
| PRINT "What is the coefficient of the term of degree "; I; | |
| INPUT A(I) | |
| NEXT I | |
| INPUT "At what X-value do you want to evaluate the polynomial "; X | |
| PRINT "The value of the polynomial at X = "; X; "is"; poly (N, A(), X) | |