Introduction to QuickBasic
Microsoft QuickBasic is a modern form of BASIC which is easy to learn. Learning QuickBasic will help you to learn Visual Basic and other programming languages.
A simplified version is QBasic. These notes give enough of QBasic for simple mathematical applications. Use the Help menu on the computer to learn more, or look at the following books:
Halvorson, M., and Rygmyr, D., 1989, Learn Basic Now, Microsoft Press. [KMUTT Mathematics Library number: COM H24]
Waite, M., Arnson, R., Gemmell, C., and Henderson, H., 1990, Microsoft Quickbasic Bible, Microsoft Press. [KMUTT Mathematics Library number: COM W7]
Contents
1. Example of a QBasic Program
REM Distance from the Origin
PRINT "Enter X and Y"
INPUT X, Y
LET Dist = SQR(X * X + Y * Y)
PRINT Dist
END
2. Lines and Keywords
A program consists of lines of text. The order of the lines in the program is the order in which the lines are read by the computer.
The lines in the program contain statements which tell the computer what to do. Each statement begins with a keyword. In the example above the keywords are
REM, PRINT, INPUT, LET, END.
The keyword REM means "remark". It states that what follows on the same line will be ignored by the computer. Remarks are useful for writing titles and subheadings in a program.
3. Constants
Numerical constants are written in the usual way, with or without a decimal point, and with or without a minus sign:
7, -54, 3.14159, -0.0065.
Numerical constants may also be written with a scale factor, which is a power of 10. The scale factor is the letter E followed by a positive or negative integer, for example:
2.75E4 means 27500, 2.75E-3 means 0.00275.
Symbolic constants are constants with a name defined by the keyword CONST, as in the following example:
CONST HalfPi = 1.570796
String constants are enclosed in quote marks. They may be used for printing messages during the running of a program. The string constant in the above example is:
"Enter X and Y".
4. Variables
A variable is the name of a location in the computer memory where a number or a string is stored. In the example above X, Y, and Dist are numerical variables.
A numerical variable consists of one or more letters, and may also contain digits. Two types of numerical variable are commonly used: integers (no decimal point), and single-precision numbers (with a decimal point and up to seven significant digits).
If the last character in the variable name is %, then the number stored is an integer. If the last character in the variable name is !, then the number stored is a single-precision number. For example,
n% is an integer variable
x! is a single-precision variable.
You may declare numerical variable types without % or ! by using the keywords DIM, AS, and INTEGER or SINGLE as follows:
DIM VariableName AS INTEGER
DIM VariableName AS SINGLE
A string variable is the name of a location in the computer memory where a string is stored. You may declare a variable to be a string by ending its name with the character $. For example, A$ is a string variable. You may also declare a string variable as follows:
DIM VariableName AS STRING
A simplified version is QBasic. These notes give enough of QBasic for simple mathematical applications. Use the Help menu on the computer to learn more, or look at the following books:
Halvorson, M., and Rygmyr, D., 1989, Learn Basic Now, Microsoft Press. [KMUTT Mathematics Library number: COM H24]
Waite, M., Arnson, R., Gemmell, C., and Henderson, H., 1990, Microsoft Quickbasic Bible, Microsoft Press. [KMUTT Mathematics Library number: COM W7]
Contents
1. Example of a QBasic Program
REM Distance from the Origin
PRINT "Enter X and Y"
INPUT X, Y
LET Dist = SQR(X * X + Y * Y)
PRINT Dist
END
2. Lines and Keywords
A program consists of lines of text. The order of the lines in the program is the order in which the lines are read by the computer.
The lines in the program contain statements which tell the computer what to do. Each statement begins with a keyword. In the example above the keywords are
REM, PRINT, INPUT, LET, END.
The keyword REM means "remark". It states that what follows on the same line will be ignored by the computer. Remarks are useful for writing titles and subheadings in a program.
3. Constants
Numerical constants are written in the usual way, with or without a decimal point, and with or without a minus sign:
7, -54, 3.14159, -0.0065.
Numerical constants may also be written with a scale factor, which is a power of 10. The scale factor is the letter E followed by a positive or negative integer, for example:
2.75E4 means 27500, 2.75E-3 means 0.00275.
Symbolic constants are constants with a name defined by the keyword CONST, as in the following example:
CONST HalfPi = 1.570796
String constants are enclosed in quote marks. They may be used for printing messages during the running of a program. The string constant in the above example is:
"Enter X and Y".
4. Variables
A variable is the name of a location in the computer memory where a number or a string is stored. In the example above X, Y, and Dist are numerical variables.
A numerical variable consists of one or more letters, and may also contain digits. Two types of numerical variable are commonly used: integers (no decimal point), and single-precision numbers (with a decimal point and up to seven significant digits).
If the last character in the variable name is %, then the number stored is an integer. If the last character in the variable name is !, then the number stored is a single-precision number. For example,
n% is an integer variable
x! is a single-precision variable.
You may declare numerical variable types without % or ! by using the keywords DIM, AS, and INTEGER or SINGLE as follows:
DIM VariableName AS INTEGER
DIM VariableName AS SINGLE
A string variable is the name of a location in the computer memory where a string is stored. You may declare a variable to be a string by ending its name with the character $. For example, A$ is a string variable. You may also declare a string variable as follows:
DIM VariableName AS STRING
5. Arrays
Variables may be used as one-dimensional arrays (vectors), or as two-dimensional arrays (matrices). The keyword DIM is used to declare the number of subscripts and their maximum values. For example, the line
DIM X(11) AS SINGLE, A(3,4) AS SINGLE
declares X(11) to be a one-dimensional array of single precision numbers with values of the subscript from 0 to 11; the variable A(3,4) is a two-dimensional array of single precision numbers with values of the first subscript from 0 to 3 and values of the second subscript from 0 to 4.
Variables may be used as one-dimensional arrays (vectors), or as two-dimensional arrays (matrices). The keyword DIM is used to declare the number of subscripts and their maximum values. For example, the line
DIM X(11) AS SINGLE, A(3,4) AS SINGLE
declares X(11) to be a one-dimensional array of single precision numbers with values of the subscript from 0 to 11; the variable A(3,4) is a two-dimensional array of single precision numbers with values of the first subscript from 0 to 3 and values of the second subscript from 0 to 4.
6. Mathematical Expressions
Mathematical expressions are written in the usual way with the symbols +, -, *, / for addition, subtraction, multiplication, and division. Multiplications and divisions are calculated before additions and subtractions, unless the order is changed by brackets. For example, if A = 1, B = 2, and C = 3, then A + B*C is 7, and (A + B)*C is 9.
Powers are represented by the symbol ^, so A^B means A to the power B. Powers are calculated before the other mathematical operations, unless the order is changed by brackets.
Mathematical expressions are written in the usual way with the symbols +, -, *, / for addition, subtraction, multiplication, and division. Multiplications and divisions are calculated before additions and subtractions, unless the order is changed by brackets. For example, if A = 1, B = 2, and C = 3, then A + B*C is 7, and (A + B)*C is 9.
Powers are represented by the symbol ^, so A^B means A to the power B. Powers are calculated before the other mathematical operations, unless the order is changed by brackets.
7. Assignment Statements
Assignment statements contain the keyword LET, a numerical variable, the symbol =, and a mathematical expression. For example, the line
LET X = A + 2.54
tells the computer to calculate A + 2.54 and store the result in the location named X. The keyword LET is optional: it may be omitted.
Assignment statements contain the keyword LET, a numerical variable, the symbol =, and a mathematical expression. For example, the line
LET X = A + 2.54
tells the computer to calculate A + 2.54 and store the result in the location named X. The keyword LET is optional: it may be omitted.
8. Input and Output
The keyword INPUT causes the computer to stop and wait for data to be entered with the keyboard. The data to be entered may consist of numerical constants separated by commas. For example, the line
INPUT A, B, C
accepts three numbers and stores them in locations named A, B, and C.
The keyword PRINT causes the computer to show output on the screen. The keyword PRINT may be followed by a list containing strings and numerical variables. The items in the list may be separated by commas or semicolons. If commas are used the items are printed with large spaces between them. If semicolons are used, the items are printed without spaces. For example, if X = 4.5, and Y = -4.5, the two lines
PRINT X,Y
PRINT "The first number is ";X
cause the following two lines to appear on the screen:
4.5 -4.5
The first number is 4.5
The keyword INPUT causes the computer to stop and wait for data to be entered with the keyboard. The data to be entered may consist of numerical constants separated by commas. For example, the line
INPUT A, B, C
accepts three numbers and stores them in locations named A, B, and C.
The keyword PRINT causes the computer to show output on the screen. The keyword PRINT may be followed by a list containing strings and numerical variables. The items in the list may be separated by commas or semicolons. If commas are used the items are printed with large spaces between them. If semicolons are used, the items are printed without spaces. For example, if X = 4.5, and Y = -4.5, the two lines
PRINT X,Y
PRINT "The first number is ";X
cause the following two lines to appear on the screen:
4.5 -4.5
The first number is 4.5
9. Built-in Functions
The following functions may be used in mathematical expressions:
ABS(X)
Absolute value of X.
SGN(X)
-1 when X<0, x="0,">0.
SQR(X)
Square root of X.
EXP(X)
Exponential X.
LOG(X)
Natural logarithm of X.
SIN(X)
Sine of X (X in radians).
COS(X)
Cosine of X (X in radians).
TAN(X)
Tangent of X (X in radians).
ATN(X)
Arctangent of X (result in radians).
The function RND is a random number in the interval [0,1). If RND is used alone, the same sequence of numbers is generated each time the program is run. This is because the numbers are "pseudo-random" numbers generated by an algorithm. You can get a different sequence each time by putting the line
RANDOMIZE TIMER
at the beginning of your program.
Floating point numbers are reduced to integers by the following functions:
INT(X!)
The largest integer less than or equal to X!
FIX(X!)
The integer part of X!
CINT(X!)
The nearest integer to X!
Examples:
INT(2.8) is 2, FIX(2.8) is 2, CINT(2.8) is 3
INT(2.1) is 2, FIX(2.1) is 2, CINT(2.1) is 2
INT(-2.1) is -3, FIX(-2.1) is -2, CINT(-2.1) is -2
10. Repetitions
A block of statements can be repeated a definite number of times using the keywords FOR, NEXT, and STEP, as in the following example:
FOR n% = 0 TO 12 STEP 3
Sum = V(n%) + V(n% + 1) + V(n% + 2)
PRINT Sum
NEXT n%
Here the variable n% takes the values 0, 3, 6, 9, and 12. If STEP 3 is omitted, then the step size is 1 and n% takes the values 0, 1, 2, ..., 11, 12. An integer variable should be used for counting the repetitions.
The keywords EXIT FOR can be used to exit before all the repetitions have been completed.
11. Conditional loops
Conditional loops can be written using the keywords DO, WHILE, and LOOP. The following form should be used:
DO WHILE condition
statements
LOOP
Here the statements are repeated while the condition is true. When the condition is false the computer jumps to the statements after LOOP. If the condition is false at the start, then the statements are not executed.
The keywords EXIT DO can be used to exit a loop before the condition has been satisfied.
The conditions can take the following forms:
X < x =" Y">= Y
X greater than or equal to Y.
X > Y
X greater than Y.
X <> Y
X not equal to Y.
Composite conditions can be written using the logical operators AND, OR, and NOT.
The following functions may be used in mathematical expressions:
ABS(X)
Absolute value of X.
SGN(X)
-1 when X<0, x="0,">0.
SQR(X)
Square root of X.
EXP(X)
Exponential X.
LOG(X)
Natural logarithm of X.
SIN(X)
Sine of X (X in radians).
COS(X)
Cosine of X (X in radians).
TAN(X)
Tangent of X (X in radians).
ATN(X)
Arctangent of X (result in radians).
The function RND is a random number in the interval [0,1). If RND is used alone, the same sequence of numbers is generated each time the program is run. This is because the numbers are "pseudo-random" numbers generated by an algorithm. You can get a different sequence each time by putting the line
RANDOMIZE TIMER
at the beginning of your program.
Floating point numbers are reduced to integers by the following functions:
INT(X!)
The largest integer less than or equal to X!
FIX(X!)
The integer part of X!
CINT(X!)
The nearest integer to X!
Examples:
INT(2.8) is 2, FIX(2.8) is 2, CINT(2.8) is 3
INT(2.1) is 2, FIX(2.1) is 2, CINT(2.1) is 2
INT(-2.1) is -3, FIX(-2.1) is -2, CINT(-2.1) is -2
10. Repetitions
A block of statements can be repeated a definite number of times using the keywords FOR, NEXT, and STEP, as in the following example:
FOR n% = 0 TO 12 STEP 3
Sum = V(n%) + V(n% + 1) + V(n% + 2)
PRINT Sum
NEXT n%
Here the variable n% takes the values 0, 3, 6, 9, and 12. If STEP 3 is omitted, then the step size is 1 and n% takes the values 0, 1, 2, ..., 11, 12. An integer variable should be used for counting the repetitions.
The keywords EXIT FOR can be used to exit before all the repetitions have been completed.
11. Conditional loops
Conditional loops can be written using the keywords DO, WHILE, and LOOP. The following form should be used:
DO WHILE condition
statements
LOOP
Here the statements are repeated while the condition is true. When the condition is false the computer jumps to the statements after LOOP. If the condition is false at the start, then the statements are not executed.
The keywords EXIT DO can be used to exit a loop before the condition has been satisfied.
The conditions can take the following forms:
X < x =" Y">= Y
X greater than or equal to Y.
X > Y
X greater than Y.
X <> Y
X not equal to Y.
Composite conditions can be written using the logical operators AND, OR, and NOT.
12. Conditional Statements
The keywords IF and THEN are used to make conditional statements as in the following example:
IF X = 0 THEN PRINT "Zero"
This causes the computer to print "Zero" when X is zero, and to jump to the next line when X is not zero. The keyword ELSE and another statement may be added to the line. For example, the line
IF X = 0 THEN PRINT "Zero" ELSE PRINT "Not zero"
causes the computer to print "Zero" when X is zero, and to print "Not zero" when X is not zero.
Blocks of conditional statements can be written with the keywords IF, THEN, ELSEIF, ELSE, and END IF as in the following example:
IF X < x =" 0">
The keywords IF and THEN are used to make conditional statements as in the following example:
IF X = 0 THEN PRINT "Zero"
This causes the computer to print "Zero" when X is zero, and to jump to the next line when X is not zero. The keyword ELSE and another statement may be added to the line. For example, the line
IF X = 0 THEN PRINT "Zero" ELSE PRINT "Not zero"
causes the computer to print "Zero" when X is zero, and to print "Not zero" when X is not zero.
Blocks of conditional statements can be written with the keywords IF, THEN, ELSEIF, ELSE, and END IF as in the following example:
IF X < x =" 0">
13. The Select Case Structure
A structure using the keywords SELECT CASE sometimes makes a block of conditional statements easier to understand than an IF block. Here is an example:
SELECT CASE Month
CASE 11, 12, 1, 2
PRINT "Cool season"
CASE 3 TO 5
PRINT "Hot season"
CASE 6 TO 10
PRINT "Wet season"
END SELECT
If Month = 11, 12, 1, or 2 (November, December, January, or February), then the words "Cool season" appear on the screen. If Month = 3, 4, or 5 (March, April, or May), then the words "Hot season" appear on the screen. If Month is any number from 6 to 10 (June to October), then the words "Wet season" appear on the screen.
14. Miscellaneous Keywords
BEEP
causes the computer to make a sound to attract the attention of the user.
STOP
causes the computer to stop running the program. (Follow the instructions on the screen to continue running.)
END
marks the end of the program.
15. Procedures
A QBasic program consists of module-level code and a number of procedures. The module-level code is the main program controlling the computer. Procedures are separate blocks of statements used by the module-level code; they can be called by the module-level code any number of times.
Procedures divide the program into easily-managed parts. They are of two kinds: SUB procedures and FUNCTION procedures.
16. SUB Procedures
A SUB procedure is used by the programmer to define a new statement keyword. A SUB procedure is written as follows:
SUB Name (parameters)
statements
END SUB
The first line shows the name of the procedure and the parameters used in the procedure. When there are no parameters the brackets after the name of the procedure are left empty.
When there is a SUB procedure in your program you must put a statement in the form
DECLARE SUB Name (parameters)
at the beginning of your module-level code. Then you can use the name of the procedure as a statement keyword anywhere in your program.
When the SUB procedure works with variables in the module-level code you must add the keyword SHARED in the DIM statement that declares the variables in the module-level code.
Here is an example of a program with a SUB procedure.
DIM SHARED X AS SINGLE, Y AS SINGLE, Z AS SINGLE
DECLARE SUB NormalizeVector ()
PRINT "Enter vector"
INPUT X, Y, Z
NormalizeVector
PRINT "The normalized vector is "; X, Y, Z
END
SUB NormalizeVector
LET N! = SQR(X * X + Y * Y + Z * Z)
X = X/N!
Y = Y/N!
Z = Z/N!
END SUB
Here the SUB procedure named NormalizeVector takes the vector (X,Y,Z) from the module-level code and replaces it by the corresponding normalized vector.
A structure using the keywords SELECT CASE sometimes makes a block of conditional statements easier to understand than an IF block. Here is an example:
SELECT CASE Month
CASE 11, 12, 1, 2
PRINT "Cool season"
CASE 3 TO 5
PRINT "Hot season"
CASE 6 TO 10
PRINT "Wet season"
END SELECT
If Month = 11, 12, 1, or 2 (November, December, January, or February), then the words "Cool season" appear on the screen. If Month = 3, 4, or 5 (March, April, or May), then the words "Hot season" appear on the screen. If Month is any number from 6 to 10 (June to October), then the words "Wet season" appear on the screen.
14. Miscellaneous Keywords
BEEP
causes the computer to make a sound to attract the attention of the user.
STOP
causes the computer to stop running the program. (Follow the instructions on the screen to continue running.)
END
marks the end of the program.
15. Procedures
A QBasic program consists of module-level code and a number of procedures. The module-level code is the main program controlling the computer. Procedures are separate blocks of statements used by the module-level code; they can be called by the module-level code any number of times.
Procedures divide the program into easily-managed parts. They are of two kinds: SUB procedures and FUNCTION procedures.
16. SUB Procedures
A SUB procedure is used by the programmer to define a new statement keyword. A SUB procedure is written as follows:
SUB Name (parameters)
statements
END SUB
The first line shows the name of the procedure and the parameters used in the procedure. When there are no parameters the brackets after the name of the procedure are left empty.
When there is a SUB procedure in your program you must put a statement in the form
DECLARE SUB Name (parameters)
at the beginning of your module-level code. Then you can use the name of the procedure as a statement keyword anywhere in your program.
When the SUB procedure works with variables in the module-level code you must add the keyword SHARED in the DIM statement that declares the variables in the module-level code.
Here is an example of a program with a SUB procedure.
DIM SHARED X AS SINGLE, Y AS SINGLE, Z AS SINGLE
DECLARE SUB NormalizeVector ()
PRINT "Enter vector"
INPUT X, Y, Z
NormalizeVector
PRINT "The normalized vector is "; X, Y, Z
END
SUB NormalizeVector
LET N! = SQR(X * X + Y * Y + Z * Z)
X = X/N!
Y = Y/N!
Z = Z/N!
END SUB
Here the SUB procedure named NormalizeVector takes the vector (X,Y,Z) from the module-level code and replaces it by the corresponding normalized vector.
17. FUNCTION Procedures
A FUNCTION procedure is used by the programmer to define a new function. It is written as follows:
FUNCTION Name (parameters)
statements
Name = expression
END FUNCTION
The first line shows the name of the function and the parameters used to calculate its value. The data type of the value of the function should be shown by a type suffix (%, !, or $) in the name. The statements below the first line give the procedure for calculating the value of the function; they must include a line assigning the calculated value to the function name.
The procedure for calculating the value of the function should not contain statements that change the values of the parameters. This is so that variables substituted for the parameters will not be changed when the function is used.
When there is a FUNCTION procedure in your program you must put a statement in the form
DECLARE FUNCTION Name (parameters)
at the beginning of your module-level code. Then you can use the function anywhere in the program.
Here is an example of a program with a FUNCTION procedure:
DIM A AS SINGLE, B AS SINGLE, C AS SINGLE
DECLARE FUNCTION Norm! (x!, y!, z!)
INPUT A, B, C
PRINT Norm!(A, B, C)
END
FUNCTION Norm! (x!, y! z!)
Norm! = SQR(x! * x! + y! * y! + z! * z!)
END FUNCTION
The output from this program is the norm of the vector you give as input when the program is run.
18. Module-Level and Local Symbolic Constants
A symbolic constant declared with the keyword CONST in the module-level code has the same value in every procedure.
A symbolic constant declared with the keyword CONST inside a procedure is local to that procedure: the same name outside the procedure does not refer to the local constant inside the procedure.
19. Module-Level and Local Variables
Variables declared in the module-level code with the keywords DIM and SHARED as follows
DIM SHARED N AS INTEGER, X AS SINGLE, A AS STRING
DIM SHARED A(3, 4) AS SINGLE, B(3) AS SINGLE
are called module-level variables. Every procedure can refer to these variables and change their values.
A variable that is used in a procedure, but is not declared with the keyword SHARED in the module-level code, is a local variable in that procedure. If the same name is used as a variable outside the procedure, the value of the local variable is not changed.
In these notes local variables and procedure parameters are written with the type characters %, !, and $. Module-level variables are declared without type characters using the keywords AS INTEGER, AS SINGLE, and AS STRING.
20. Sequential Data Files
A sequential data file is a list of numbers or strings stored on a disk. To use a data file you must first open it using the keyword OPEN as follows:
OPEN FileName FOR Mode AS #Number
FileName is the name of the file.
Mode is one of the following:
OUTPUT
for sending new data to the file (existing data are lost),
INPUT
for getting data from the file,
APPEND
for adding data to the file (existing data remain in the file).
#Number is #1, #2, etc, to identify the file in the program.
WRITE #1 is used to send numerical or string data to file #1.
INPUT #1 is used to get data from file #1.
EOF, which means "End Of File", is used to test whether or not all the data have been read from a file. EOF is true when the end of the file has been reached; otherwise EOF is false.
After you have used a file you must close it. CLOSE #1 closes file #1. The keyword CLOSE used alone closes all files.
21. Graphics
The graphics you can do depends on the monitor and the graphics adapter card you have in your system. The keyword SCREEN is used to set the resolution and the number of colors in the display.
PSET is used to plot points, and PRESET is used to erase points. LINE may be used for plotting straight lines and rectangular boxes. CIRCLE may be used for plotting circles and ellipses.
VIEW and VIEW PRINT are used to set areas of the display for graphics and text output.
WINDOW may be used to set the coordinates used for plotting points.
EXAMPLES OF PROGRAMS
Example 1:
REM Arithmetic Sequences
PRINT "Enter first term"
INPUT A
PRINT "Enter common difference"
INPUT D
PRINT "Enter number of terms"
INPUT N
PRINT "The sequence is"
T = A
S = 0
FOR i% = 1 TO N
PRINT T
S = S + T
T = T + D
NEXT i%
PRINT "The sum is "; S
Example 2:
REM Hyperbolic Functions
DECLARE FUNCTION Sinh! (X!)
DECLARE FUNCTION Cosh! (X!)
PRINT "Enter T"
INPUT T
PRINT Sinh!(T), Cosh!(T)
END
FUNCTION Cosh! (X!)
Cosh! = .5 * (EXP(X!) + EXP(-X!))
END FUNCTION
FUNCTION Sinh! (X!)
Sinh! = .5 * (EXP(X!) - EXP(-X!))
END FUNCTION
Example 3:
REM Arctan in Degrees
DECLARE SUB ComputeArctan (X!, Y!)
DIM SHARED Theta AS SINGLE
PRINT "Enter X and Y"
INPUT X, Y
ComputeArctan X, Y
PRINT Theta
END
SUB ComputeArctan (X!, Y!)
IF X!=0 THEN
Theta = 90*SGN(Y!)
ELSEIF X!>0 THEN
Theta = 57.29577*ATN(Y!/X!)
ELSEIF Y!=0 THEN
Theta = 180
ELSE
Theta = 180*SGN(Y!) + 57.29577*ATN(Y!/X!)
END IF
END SUB
Example 4:
REM Transformation of Vectors
DIM SHARED x AS SINGLE, y AS SINGLE
DECLARE SUB TransformVector (a11!, a12!, a21!, a22!)
PRINT "Enter vector x,y"
INPUT x, y
PRINT "Enter a11, a12, a21, a22"
INPUT a!, b!, c!, d!
TransformVector a!, b!, c!, d!
PRINT "The transformed vector is "; x, y
END
SUB TransformVector (a11!, a12!, a21!, a22!)
LET u! = a11! * x + a12! * y
LET v! = a21! * x + a22! * y
x = u!
y = v!
END SUB
By Ralph Engineer programmer
A FUNCTION procedure is used by the programmer to define a new function. It is written as follows:
FUNCTION Name (parameters)
statements
Name = expression
END FUNCTION
The first line shows the name of the function and the parameters used to calculate its value. The data type of the value of the function should be shown by a type suffix (%, !, or $) in the name. The statements below the first line give the procedure for calculating the value of the function; they must include a line assigning the calculated value to the function name.
The procedure for calculating the value of the function should not contain statements that change the values of the parameters. This is so that variables substituted for the parameters will not be changed when the function is used.
When there is a FUNCTION procedure in your program you must put a statement in the form
DECLARE FUNCTION Name (parameters)
at the beginning of your module-level code. Then you can use the function anywhere in the program.
Here is an example of a program with a FUNCTION procedure:
DIM A AS SINGLE, B AS SINGLE, C AS SINGLE
DECLARE FUNCTION Norm! (x!, y!, z!)
INPUT A, B, C
PRINT Norm!(A, B, C)
END
FUNCTION Norm! (x!, y! z!)
Norm! = SQR(x! * x! + y! * y! + z! * z!)
END FUNCTION
The output from this program is the norm of the vector you give as input when the program is run.
18. Module-Level and Local Symbolic Constants
A symbolic constant declared with the keyword CONST in the module-level code has the same value in every procedure.
A symbolic constant declared with the keyword CONST inside a procedure is local to that procedure: the same name outside the procedure does not refer to the local constant inside the procedure.
19. Module-Level and Local Variables
Variables declared in the module-level code with the keywords DIM and SHARED as follows
DIM SHARED N AS INTEGER, X AS SINGLE, A AS STRING
DIM SHARED A(3, 4) AS SINGLE, B(3) AS SINGLE
are called module-level variables. Every procedure can refer to these variables and change their values.
A variable that is used in a procedure, but is not declared with the keyword SHARED in the module-level code, is a local variable in that procedure. If the same name is used as a variable outside the procedure, the value of the local variable is not changed.
In these notes local variables and procedure parameters are written with the type characters %, !, and $. Module-level variables are declared without type characters using the keywords AS INTEGER, AS SINGLE, and AS STRING.
20. Sequential Data Files
A sequential data file is a list of numbers or strings stored on a disk. To use a data file you must first open it using the keyword OPEN as follows:
OPEN FileName FOR Mode AS #Number
FileName is the name of the file.
Mode is one of the following:
OUTPUT
for sending new data to the file (existing data are lost),
INPUT
for getting data from the file,
APPEND
for adding data to the file (existing data remain in the file).
#Number is #1, #2, etc, to identify the file in the program.
WRITE #1 is used to send numerical or string data to file #1.
INPUT #1 is used to get data from file #1.
EOF, which means "End Of File", is used to test whether or not all the data have been read from a file. EOF is true when the end of the file has been reached; otherwise EOF is false.
After you have used a file you must close it. CLOSE #1 closes file #1. The keyword CLOSE used alone closes all files.
21. Graphics
The graphics you can do depends on the monitor and the graphics adapter card you have in your system. The keyword SCREEN is used to set the resolution and the number of colors in the display.
PSET is used to plot points, and PRESET is used to erase points. LINE may be used for plotting straight lines and rectangular boxes. CIRCLE may be used for plotting circles and ellipses.
VIEW and VIEW PRINT are used to set areas of the display for graphics and text output.
WINDOW may be used to set the coordinates used for plotting points.
EXAMPLES OF PROGRAMS
Example 1:
REM Arithmetic Sequences
PRINT "Enter first term"
INPUT A
PRINT "Enter common difference"
INPUT D
PRINT "Enter number of terms"
INPUT N
PRINT "The sequence is"
T = A
S = 0
FOR i% = 1 TO N
PRINT T
S = S + T
T = T + D
NEXT i%
PRINT "The sum is "; S
Example 2:
REM Hyperbolic Functions
DECLARE FUNCTION Sinh! (X!)
DECLARE FUNCTION Cosh! (X!)
PRINT "Enter T"
INPUT T
PRINT Sinh!(T), Cosh!(T)
END
FUNCTION Cosh! (X!)
Cosh! = .5 * (EXP(X!) + EXP(-X!))
END FUNCTION
FUNCTION Sinh! (X!)
Sinh! = .5 * (EXP(X!) - EXP(-X!))
END FUNCTION
Example 3:
REM Arctan in Degrees
DECLARE SUB ComputeArctan (X!, Y!)
DIM SHARED Theta AS SINGLE
PRINT "Enter X and Y"
INPUT X, Y
ComputeArctan X, Y
PRINT Theta
END
SUB ComputeArctan (X!, Y!)
IF X!=0 THEN
Theta = 90*SGN(Y!)
ELSEIF X!>0 THEN
Theta = 57.29577*ATN(Y!/X!)
ELSEIF Y!=0 THEN
Theta = 180
ELSE
Theta = 180*SGN(Y!) + 57.29577*ATN(Y!/X!)
END IF
END SUB
Example 4:
REM Transformation of Vectors
DIM SHARED x AS SINGLE, y AS SINGLE
DECLARE SUB TransformVector (a11!, a12!, a21!, a22!)
PRINT "Enter vector x,y"
INPUT x, y
PRINT "Enter a11, a12, a21, a22"
INPUT a!, b!, c!, d!
TransformVector a!, b!, c!, d!
PRINT "The transformed vector is "; x, y
END
SUB TransformVector (a11!, a12!, a21!, a22!)
LET u! = a11! * x + a12! * y
LET v! = a21! * x + a22! * y
x = u!
y = v!
END SUB
By Ralph Engineer programmer
No comments:
Post a Comment