Getting Started
Introduction
Sample Programs
QBasic IDEs
History
Advice
Tools
Mini-Tutorial
Tutorial
Code Snippets

Resources
Web Sites
More Tutorials
Vendors
Books
Magazines
NewsLetters
NewsGroups
Forums
User Groups
Talk Shows
Blogs

GBIC >> QBasic >> Tutorial >> Declarations

QBasic Information Center Tutorials - Declarations
These tutorials were written to help you get a quick, but thorough, understanding of QBasic - the scope of the language as well as it's specific capabilities.

Declarations
QBasic does not require that variables be declared before being used. However, it is considered good practice to declare variables before their use. QBasic provides several ways of declaring variables, as shown in the following table of keywords.

    • Variable Declaration
     dim, redim, const 
    • By-Letter Typing    
     defint, defdbl, defsng, defstr, deflng
    • Scope Control    
     static, shared 
    • User-Defined Type    
     type 

Declaration by Use
Since QBasic does not require that variables be declared before being used, a variable can be created by simply using it in a line of code. QBasic will assign numbers an initial, default value of zero and strings a default value of "" (empty string).

     a$ = 5      # a$ is created simply by assigning it a value
     b$ = c$     # if c$ has not been defined, b$ is assigned a value of ""

When creating a variable by simply using it, the data type can be declared by adding on of the data type suffixes ($, %, &, !, or #) to the variable name. If no suffix is added, QBasic considers the variable to be a type single.

       x$ = "hello"   ' $ suffix defines a string
       y% = 1         ' % suffix defines an integer
       y& = 123       ' & suffix defines a long integer
       y! = 1.2       ' ! suffix defines a single
       y# = 122.34    ' # suffix defines a double

Declaration Function Reference
Here's a quick reference of the available declaration functions, in alphabetical order.

  • DefXXX - Declares any variable beginning with specific letters to be a certain data type by default.
        DefInt i-k       # declares all variables beginning with i,j,k as integers
        DefSng r-t       # declares all variables beginning with r,s,t as single
        

    These define DefFn and Function data types as well.

  • dim - Declares a variable, and optionally defines its type
        Dim x           'defines variable x, does not assign type
        Dim x as long   'defines x as type long
        

  • const - Defines a variables whose value does not change. This is useful in speeding up programs by assigning values which the compiler places directly into the compiled code.
        Const x = 5     'defines a variable x whose value is 5
        x = 2           'error because Const values cannot be reassigned
        

  • redim - Declares an array variable, erases an prior value assignments, and optionally changes the variable's type
        DIM x(5)       'defines array with upper index of 5
        x(1) = 2       'x(1) is set to value of 2
        Redim x(10)    'x(1) is no zero
        

  • shared - Gives procedures (subroutines/functions) access to module-level variables.
        Shared X as long
        

  • static - Used inside procedures (subroutines/functions) to make a variable local in scope to the procedure. It also preserves the value of the variable between calls to the procedure.
        Static X as long
        

  • type - Creates a data type consisting of one or more of the standard data types. In this example, a "Custom" data type is defined and the variable X is defined as that data type. The example shows how to assign and access the elements which make up a user-defined data type.
            Type Custom 
                a as string
                b as number
                c as double
            End Type
    
            Dim X as Custom
    
            X.a = "hello"
            X.b = 5
            X.c = 53.2429965
       

If you have any suggestions for additions to these tutorials, please let me know.