Q(uick)BASIC Statements: DEFtype
Quick View
DEFtype
BASIC declarations that set the default data type for variables, DEF FN functions, and FUNCTION procedures
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- DEFINT letterrange [,letterrange]…
- DEFLNG letterrange [,letterrange]…
- DEFSNG letterrange [,letterrange]…
- DEFDBL letterrange [,letterrange]…
- DEFSTR letterrange [,letterrange]…
Description/Parameter(s)
letterrange | A letter or range of letters (such as A-M). QBasic sets the default data type for variables, DEF FN functions, and FUNCTION procedures whose names begin with the specified letter or letters as follows: |
Statement | Default Data Type |
DEFINT | Integer |
DEFLNG | Long integer |
DEFSNG | Single precision |
DEFDBL | Double precision |
DEFSTR | String |
- A data-type suffix (%, &, !, #, or $) always takes precedence over a DEFtype statement.
- Single-precision is the default data type if you do not specify a DEFtype statement.
- After you specify a DEFtype statement in your program, QBasic automatically inserts a corresponding DEFtype statement in each procedure you create.
Differences from BASICA
- If BASICA encounters a variable without an explicit type (indicated by !, #, &, $, or %), it uses the default type set by the most recent DEFtype statement. For example, the type of the variable IFLAG changes from integer to single precision in the following BASICA code fragment:
- 10 DEFINT I
- 20 PRINT IFLAG
- 30 DEFSNG I
- 40 GOTO 20
- In QBasic, a variable's type cannot be changed after it appears in a program.
Example
DEFDBL A-Z
a = SQR(3)
PRINT "Square root of 3 = "; a
Syntax
- DEFINT letterrange [,letterrange]…
- DEFLNG letterrange [,letterrange]…
- DEFSNG letterrange [,letterrange]…
- DEFDBL letterrange [,letterrange]…
- DEFSTR letterrange [,letterrange]…
Description/Parameter(s)
The letterrange has the form:
- letter1[-letter2]
where letter1 and letter2 are any of the uppercase or lowercase letters of the alphabet. Names beginning with the letters in letterrange have the type specified by the last three letters of the statement: integer (INT), long integer (LNG), single precision (SNG), double precision (DBL), or string (STR). For example, in the following program fragment, Message is a string variable:
- DEFSTR A-Q
- ⋮
- Message="Out of stack space."
The case of the letters in letterrange is not significant. All of the following statements are equivalent:
- 10 DEFINT I
- 20 PRINT IFLAG
- 30 DEFSNG I
- 40 GOTO 20
A type-declaration character (%, &, !, #, or $) always takes precedence over a DEFtype statement. DEFtype statements do not affect record elements.
Note: I!, I#, I&, I$, and I% are all distinct variables, and each may hold a different value. |
Differences from BASICA
- BASICA handles DEFtype statements differently. BASICA scans a statement each time before executing it. If the statement contains a variable without an explicit type (indicated by !, #, &, $, or %), the interpreter uses the current default type.
- In the example below, when BASICA interprets line 20, it determines that the current default type for variables beginning with I is integer. Line 30 changes the default type to single precision. When BASICA loops back to line 20, it rescans the line and uses IFLAG as a single-precision variable.
- In contrast, QuickBASIC scans the text only once; once a variable appears in a program line, its type cannot be changed.
Example
Syntax
- DEFINT letterrange [,letterrange]…
- DEFLNG letterrange [,letterrange]…
- DEFSNG letterrange [,letterrange]…
- DEFDBL letterrange [,letterrange]…
- DEFCUR letterrange [,letterrange]…
- DEFSTR letterrange [,letterrange]…
Description/Parameter(s)
- The argument letterrange has the syntax:
letter1[-letter2] | letter1 and letter2 are any of the uppercase or lowercase letters of the alphabet. |
Usage Notes
- DEFtype sets the default data type. For example, in the following program fragment, Message is a string variable:
- DEFSTR A-Q
- ⋮
- Message = "Out of stack space."
- The case of the letters in letterrange is not significant. These three statements are equivalent:
- DEFINT I-N
- DEFINT I-N
- DEFINT I-N
- A type-declaration character (%, &, !, #, @ or $) always takes precedence over a DEFtype statement. DEFtype statements do not affect record elements.
Important
- I%, I&, I!, I#, I@, and I$, all are distinct variables, and each may hold a different value.
Differences from BASICA
- BASICA handles default data types differently. BASICA scans each statement before executing it. If the statement contains a variable without an explicit type (indicated by %, &, !, #, @ or $), the interpreter uses the current default type. In contrast, the current version of BASIC scans the text once only and after a variable appears in a program line, its type cannot be changed.
Example
This example finds the cube root of an input value. It uses the ABS function to test if the current guess is accurate. The DEFDBL statement is used to establish the default for the variables.
DEFDBL A-Z
Precision = .0000001#
CLS 'Clear the screen.
INPUT "Enter a value: ", Value 'Prompt for input.
'Make the first two guesses.
X1 = 0#: X2 = Value
'Loop until the difference between guesses is
'less than the required precision.
DO UNTIL ABS(X1 - X2) < Precision
X = (X1 + X2) / 2#
' Adjust the guesses.
IF X * X * X - Value < 0# THEN
X1 = X
ELSE
X2 = X
END IF
LOOP
PRINT "The cube root is "; X
Sample Output:
Enter a value: 27 The cube root is 2.99999997206032