Subs and variables

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
johnfin
Coder
Posts: 18
Joined: Tue Apr 01, 2008 8:26 am

Subs and variables

Post by johnfin »

It seems that I am loosing my variables while popping back and forth between subs. I have inputs in the various sub routines yet when I try to access all of them they come up as zero. What am I doing wrong.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Are you passing those variables in the parameter list? Variables not passed must be shared for any Declared Sub programs.

DIM SHARED cost AS SINGLE

You can also DIM arrays as SHARED

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
johnfin
Coder
Posts: 18
Joined: Tue Apr 01, 2008 8:26 am

variables

Post by johnfin »

Thanks Ted, I'll try the shared command.
TmEE
Veteran
Posts: 97
Joined: Mon Mar 17, 2008 11:14 am
Location: Estonia, Rapla
Contact:

Post by TmEE »

I use COMMON SHARED for variables.
Mida sa loed ? Nagunii aru ei saa :P
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

COMMON SHARED is also useable with chained modules that are compiled using BRUN.EXE. It is simpler than SHARED, but SHARED allows you to define the variable type without using type suffixes also.

If more than one module needs the same list of COMMON SHARED each module must use it in the same order.

COMMON SHARED A%, B%, etc.

DIM SHARED A AS INTEGER, B AS INTEGER, etc.

You can also define the type in the SUB parameter list:

DECLARE SUB ( A AS INTEGER, B AS INTEGER)

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
johnfin
Coder
Posts: 18
Joined: Tue Apr 01, 2008 8:26 am

Jumping

Post by johnfin »

I have a main module with a bunch of subs. I jump sub to sub to sub but need to know how to return to the top of the main program. I tried a goto line # or label and it cant find it. I even tried to share the label name but it cant find the main program. Any ideas?
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

When you use a SUB (consider it as a sub-program), the main program simply goes to the address of that SUB, the SUB executes its code, and, at END SUB, the program simply returns to the next line from where it took off for the SUB. So, if you execute one or more SUBS, and want to end up someplace different than "the next line" after the line that calls the SUB, just add a new line to the program, right after that call, such as:
GOTO TOP
and add the label
TOP:

at the top of your main progam.

If you use a GOSUB someLabel, the program will go to the address of that label, that is, to
someLabel:
execute the code there until it hits the RETURN at the end of that subroutine, and then it returns to the next line to the GOSUB someLabel, and continues there. Here, if you want to go to the top of the program, you must add the line, GOTO TOP, and add the label TOP: at the top of the main program.
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

You can have GOSUB or GOTO calls in the Declared SUB, but the line numbers or names are in that SUB also.

I don't think GOTO is worth my time except for ON ERROR GOTO.

Try to figure a better way to return to a line by writing the program progressively downward until a loop takes it back to the "beginning" of the part you need.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply