Page 1 of 1

Subs and variables

Posted: Fri Apr 04, 2008 8:05 am
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.

Posted: Fri Apr 04, 2008 9:52 am
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

variables

Posted: Fri Apr 04, 2008 1:27 pm
by johnfin
Thanks Ted, I'll try the shared command.

Posted: Fri Apr 04, 2008 1:31 pm
by TmEE
I use COMMON SHARED for variables.

Posted: Fri Apr 04, 2008 1:52 pm
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

Jumping

Posted: Fri Apr 04, 2008 2:05 pm
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?

Posted: Fri Apr 04, 2008 5:16 pm
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.

Posted: Fri Apr 04, 2008 9:28 pm
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