------------------ CodeFAQ Part 2 of 11 -------------------------
The phrase "STRING * 20" means that 20 characters are reserved
for the sub-variable Text. Anything more than that assigned
to Text will just be thrown away.
Now, just like QB's "integer" type doesn't mean anything until
it is assigned to a variable, your TYPEs don't do anything
unless you define a variable as that TYPE:
DIM Scr AS ScreenType
At this point, you can use each of the elements (or sub-
variables as I have called them) as if they were individual
elements:
Scr.X = 13
Scr.Y = 50
Scr.Color = 1
Scr.Text = "Hello, world!"
LOCATE Scr.X, Scr.Y
COLOR Scr.Color
PRINT Scr.Text
Of course, you can't assign anything but an integer to
Scr.X, because the element X was defined as an integer.
Similarly, you can only put strings of 20 or fewer characters
into Scr.Text because Text was defined as a string with 20
chars.
2) WHAT ARE SUBS AND FUNCTIONS, AND HOW DO I USE THEM?
SUBs and FUNCTIONs are very useful features in QB; these are
very versatile and have many purposes and uses. They have
developed as a method of "modular programming," in which
programming statements that form a routine are kept separate
from other routines in order to organize the source code and
help you, the programmer, read your code more easily.
SUBroutines: A SUB is a separated section of code that can be
run at any time in a QB program. A SUB groups together state-
ments that work together to perform a certain task, such as
getting input or performing calculations. Using a SUB allows
you to read your code more easily, to perform the same task as
many times as necessary (as opposed to typing the same code
over and over), and to (easily) use the same subroutine in
more than one program.
You can create a SUB by typing the following statement anywhere
in a QB program:
SUB NewSub
The SUB name must start with a letter, but then can use letters
or numbers. Once you press Enter, you will be moved by QB into
a new section of code, labeled "Filename.BAS:NewSub" at the top
of the screen. (Note: Press F2 at any time to switch between
your main program and any SUBs or FUNCTIONs.) In this new area,
you can enter any normal statements like you would in the main
part of the program. Any time you want that SUBroutine to be
run, enter one of the following commands:
CALL NewSub
NewSub
Each of these does the same thing: control of the program is
transferred to SUB NewSub, and the statements in that SUB are
run until the line END SUB is reached. (By the way, QB auto-
matically puts that line in there when you create a SUB.) You
can even call a SUB from inside another SUB, or create
recursive algorithms..the possibilities are endless!
Any variables you use in the SUB are "local" to that SUB. In
other words, the variable names (such as Name$) mean nothing
outside of that SUB. You can even have a variable called
Name$ in the main part of the program, and the two will be
kept separate. But suppose you want your new SUB to use the
variable Name$ as it exists in the main program. How can you
"pass" that variable to the SUB? Simply add it to the calling
statement:
CALL NewSub(Name$)
NewSub Name$
Any number of variables can be passed to a SUB:
CALL NewSub(Name$, Text$, File$)
NewSub Name$, Text$, File$
However, the SUB statement you made at the very beginning
must match *exactly* the TYPEs and number of variables in
the calling statement:
-------------- CodeFAQ ends Part 2 of 11 ------------------------
Robert (Bob) Kohl Rio Rancho, New Mexico
Home Page: http://www.geocities.com/SiliconValley/Way/7854
http://members.tripod.com/~Bob_Kohl/index.html
Internet: bobakohl@abq.com bobakohl1@juno.com barbarianh@aol.com
--- Blue Wave/DOS v2.30
(1:301/45)
---------------
* Origin: * Binary illusions BBS * Albuquerque, NM * 505.897.8282 *
|