Need help with subs!!!

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
crazydrivr
Newbie
Posts: 8
Joined: Sat May 06, 2006 8:32 pm

Need help with subs!!!

Post by crazydrivr »

I am making an RPG game that uses money, and I put the shops in a sub. I need a way to total the money i picked up in one sub or the main module and transfer it to the shop subs. So basically a way to transfer a variable from one sub to another.
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

You could:

-Use COMMON
-Use DIM SHARED
-Use STATIC varibles
-Convert your subs to a function (I recommend this: easiest to work with)

If you don't know any of those statements, look on QB's online help or search google.
Image
User avatar
Theophage
Coder
Posts: 44
Joined: Sun May 07, 2006 7:32 pm
Location: Tucson, AZ
Contact:

Post by Theophage »

What you've got to understand is the difference between a local variable and a global variable. When your program is all in one module, both types act the same, but when your program is broken up into a main module and subs, the difference becomes apparent.

Any variable used in either the main module or any of the subs is automatically a local variable. This means that only this section of the program can use the value of this variable. If you've got a variable like Gold=200 in your main program, and you try to say print Gold in one of your subs, the sub would print zero.

A global variable is one that all parts of the program recognize. You define these in your main module like this:

DIM SHARED Gold AS INTEGER

Now that the variable "Gold" is shared, you can assigning it a value in any part of your program, and any other part of your program can use that value.
Daniel "Theophage" Clark
theophage (at) geocities (dot) com

"God used to be my co-pilot, but our plane crashed in the mountains and I had to eat Him..."
Post Reply