Variable handling?

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
Rocket Boy
Coder
Posts: 19
Joined: Thu Sep 08, 2005 3:14 am

Variable handling?

Post by Rocket Boy »

I need to know how I can not lose the value of a variable when leaving a sub, so that I can go back to it and access that value again.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

look up the STATIC keyword.. or the SHARED keyword..
I have left this dump.
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations »

Code: Select all

DECLARE SUB yoursub ()
DIM SHARED yourvariable
CALL yoursub
PRINT yourvariable

SUB yoursub()
LET yourvariable = 4
END SUB
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

A more common method is to use a parameter list with the sub, like so:

Code: Select all

DECLARE SUB yoursub (yourvariable)
CALL yoursub (subvariable)
PRINT subvariable

END

SUB yoursub (yourvariable)
LET yourvariable = 4
END SUB
The SUB doesn't use the value of "subvariable" from the CALL, which the SUB refers to as "yourvariable".
However, it does modify "yourvariable" to 4 which will be returned to the CALL with "subvariable" equal to 4.
*****
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 can also use the common statement, but thats really messy and un-good... use it like this... i ferget how to use it though, so check qbs help.[/code]
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Nathan1993 wrote:You can also use the common statement, but thats really messy and un-good... use it like this... i ferget how to use it though, so check qbs help.[/code]
Here's the definition of the COMMON statement:

Code: Select all

COMMON - a non-executable statement that declares global variables for 
         sharing between modules, or for chaining to another program
It does not apply to the issue of this thread.
*****
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} »

yes it does (kinda) because it makes a global varible and a global varible doesn't loose its value when you exit a sub that it is used in...

:roll: :roll: :roll:

i think, anyway...

:shock: :shock: :? now im just confusing myself...
Image
Post Reply