By NetherGoth
Look any commands that you don't know up in
the QB help file. Qbasic has long been viewed as an inferior or
downright bad language. One reason for this is because it can be
unstructured. An unstructured program is one where you have all the
programming code in one program or file. Although the program will
still work, the problem is that you have to keep on using GOTO, or
GOSUB commands, to skip between parts of your program. This is bad for
several reasons. Firstly, it makes your programs incredibly messy.
Because of this, they are very difficult to debug and check for errors.
Because of this, they contain more bugs, and are therefore overall
worse programs. Also, if you are using GOTO, you usually have to waste
quite a bit of time and space reprogramming sections or adding code to
allow jumping to different sections. Then, on the other hand, there is
structured programming. This makes use of what are called SUBs.
SUB is short for SubProcedure. This is basically like
a smaller program, inside your main program. What this allows you to do
is make lots of small sections, and test them seperately, so you can
find errors and bugs faster. Also, if you have a sub, and you want to
have whatever is in the sub happen more than once, all you have to do
is call the sub again, which saves space, and just makes everything
easier. This tutorial is designed for both those who have never heard
of SUBs before, and those who just want to increase their knowledge.
Firstly,
you need to have either Qbasic 1.1 or Quickbasic 4.0/4.5 running. The
main screen you see now is called the Main Module. This is where the
main program will be, branching off to all the SUBs. Now go to the EDIT
menu, and choose New SUB. Type a name for the SUB. Now you should see
the words SUB (name you gave), and underneath, END SUB. This is just
showing where the start and end of the SUB is. Now go to the VIEW menu,
and choose SUBs. A window should now open. There should be a list of
names in the window. Right now, your list should have only two names.
The name of your program, and the name of your SUB. Click on the name
of your program (which might be Untitled if you haven't given it a
name). Now click on Edit in Active. You should see the main program
again. Now you should know how to create and switch between SUBs and
the Main Module. Another way to do this is to go to the View menu, and
choose Next SUB, which will show the next SUB on the list. Now, its
time to actually use these SUBs.
Go to your main module. Now type PRINT "HELLO". On
the next line type the name of your SUB. Now go back to the SUB, and in
between the start and end markers, type PRINT "There". Now run the
program. The words "Hello There" Should appear on the screen. What you
did was run the main module, then switch to the SUB and run that too.
The problem is that now you can't get back to your main module. to fix
this, use EXIT SUB. Go to your main module, and, underneath the place
where you called your SUB, type PRINT "Everyone". Go back to your SUB,
and right underneath the PRINT "There", type EXIT SUB. Now run the
program. You should now see the words Hello There Everyone appear. What
you have done is printed Hello, called your SUB, printed There, gone
back to the main module right below where the SUB was called, and
printed "Everyone". Although this is a simple example, it shows how to
use SUBs in your programs. For example, if you were making an RPG, and
you wanted to have monsters move around. Instead of placing this all in
your main program, you can put the monster moving area in a SUB. That
way, whenever monsters need to move, you can just type the name of the
SUB, and it will be executed. These are the basics of using SUBs.
This section is for people more advanced
than the absolute beginner. In order to use SUBs properly, you need to
make sure that all your programs' variables are working properly.
Normally, if you say that variable var1 = 10, then var1 will be
created, and it will equal to ten. If you say DIM hello%(100), then
hello will be created as an array with 100 slots. However, with SUBs,
whenever you enter a SUB, variables are not carried over. Because of
this, if you have a certain variable telling a command inside a SUB to
do something, it will be erased as soon as the SUB is called, and your
program will not work. In order to stop this happening, you can do one
of two things. First, you can define what variables are to be used in
what SUBs. To do this, go to your main module. At the top you should
have DECLARE SUB name(). This is telling the program that this SUB
exists. Now, in between the two ( ), type the name of a variable. This
variable will now be carried over to the SUB, and will not be erased.
The problem with this method, however, is that if you have lots of
SUBs, you end up wasting time and space typing names of variables over
and over again. The second way is to use DIM SHARED. Now, as you know,
DIM is used to give dimensions to variables or arrays. If you type DIM
SHARED, however, any variable after DIM SHARED will be carried over to
all existing SUBs. So if you want to have 5 variables carried over, you
type:
DIM SHARED life, mana, dex, strength, wisdom
This makes sure that these 5 variables will not be
erased. This works the same for arrays. Also, you can't use DIM SHARED
inside a SUB, only in the main module. This is probably because when
you use DIM SHARED, you are defining the variable to be used in all
SUBs, including the one it is in. Therefore, DIM SHARED would go into
some kind of loop, since it would be redefining the SUB that it was in
and was running. Another note is that you can use CALL in front of the
SUB name, as another way to run the SUB. This is just wasted space
though, since it does the same thing as just typing the sub name. Also,
since SUBs are structured, you can't use any of the commands like GOTO
and GOSUB in a SUB. Although you can't use DIM SHARED inside a SUB, you
can use STATIC, followed by the name(s) of any variables. These
variables overwrite any variables of the same name that were defined by
DIM SHARED. So basically, STATIC is like DIM SHARED, except it can only
be used inside a SUB, and it overwrites variables in the main code.
nethersoft@hotmail.com
www.fortunecity.com/skyscraper/terabyte/277
Back to Top
This tutorial originally appeared in QBasic: The Magazine Issue 2.