Subs and variables
Subs and variables
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.
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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
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
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
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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
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
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
Jumping
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?
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.
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
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
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
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
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