Microsoft QBasic
Click here for site map

Home
Intro
Lessons
Library
Reference
About
www.microsoft.com

This is a line. . .a short one

Lesson 8

This is a line. -------- If you don't know what a line is, you're brain dead.

What you will learn in this lesson
  • All about Subs

Lesson 8

This is a line. -------- If you don't know what a line is, you're brain dead.

Subs are portions of QBasic set apart from the rest of the program. Usually they perform specific tasks.

Here's how to define, or make QBasic recognize a sub :

Raw Code

SUB name[(parameterlist)] [STATIC]
  [statementblock]
END SUB

    name            The name of the SUB procedure, up to 40 characters
                    long, with no data type suffix.
    parameterlist   One or more variables that specify parameters to be
                    passed to the SUB procedure when it is called:

                    variable[( )] [AS type] [, variable[( )] [AS type]]...

                    variable    A Basic variable name.
                    type        The data type of the variable (INTEGER,
                                LONG, SINGLE, DOUBLE, STRING, or a
                                user-defined data type).

    STATIC          Specifies that the values of the SUB procedure's
                    local variables are saved between function calls.

Another command called Declare is also used :

SUB name[(parameterlist)] [STATIC]
[CALL] name [([argumentlist])]
DECLARE {FUNCTION | SUB} name [([parameterlist])]

    name             The name of the procedure.
    parameterlist    One or more variables that specify parameters to be
                     passed to the procedure when it is called:

                     variable[( )] [AS type] [, variable[( )] [AS type]]...

                     variable    A Basic variable name.
                     type        The data type of the variable (INTEGER,
                                 LONG, SINGLE, DOUBLE, STRING, or a
                                 user-defined data type). ANY allows any
                                 data type.

And to use a sub, we use the Call command as below :


SUB name[(parameterlist)] [STATIC]
[CALL] name [([argumentlist])]

    name            The name of the SUB procedure to call.
    argumentlist    The variables or constants to pass to the SUB
                    procedure. Separate multiple arguments with commas.
                    Specify array arguments with the array name followed
                    by empty parentheses.

    A Sub can be used without the Call keyword.
    If you omit the Call keyword, also omit the parentheses around
    argumentlist. Either declare the procedure in a DECLARE statement
    before calling it, or save the program and QBasic automatically
    generates a Declare statement.
    To specify an argument whose value will not be changed by the
    procedure, enclose the argument in parentheses.

Finally, the Shared command specifies which variables to be used or shared in and between the Subs and the program.

Shared gives procedures access to module-level variables.
Static makes a variable local to a function or procedure and preserves its
value between calls.

SHARED variable[()] [AS type] [,variable[()] [AS type]]...
STATIC variable[()] [AS type] [,variable[()] [AS type]]...

    variable    The name of the module-level variable to share or variable
                to make static. Variable names can consist of up to 40
                characters and must begin with a letter. Valid characters
                are A-Z, 0-9, and period (.).
    AS type     Declares the data type of the variable (INTEGER, LONG,
                SINGLE, DOUBLE, STRING, or a user-defined type).

Back to Top


Well, I suppose that's all for the lesson. Now for the Assignment!


QB Assignment

Today's Assignment is simply to have fun, experiment, and practise using the Sub commands learnt.

Have fun!

Back to Module 2

This is a line. -------- If you don't know what a line is, you're brain dead.

This is an imagemap menubar. Hey I think you know what an imagemap is.

Home Intro Lessons Library Reference About
[ Home ] [ Intro ] [ Lessons ] [ Library ] [ Reference ] [ About ]

This is a line. -------- If you don't know what a line is, you're brain dead.

This Webpage is ThinkQuest entry Unknown.
Email: lccorp1997@hotmail.com

This Webpage is Designed for Netscape Navigator 3.0 and MSIE 3.0
We prefer a 24-bit True Color mode with 640x480 screen mode.
This Webpage created for Thinkquest 1998.