
Simple question, Please Help!!! :)))
Quote:
>Doing this rather silly school project for which we were requested to
>use subroutines (with little guidance).
>I'm using the command:
>"GoSub ControlBrk"
>I now need to program the ControlBrk part of my program and I was
>thinking that would go something like this....
>Sub ControlBrk <--- what's the Syntax for this????
> code
> code
> code
>Return
>(which will return control to the statement following my GoSub
>command)
>Well I keep getting compiler errors and the darn thing won't run.
You didn't mention which flavor of BASIC you use, but if you have a compiler
I'll guess it's QuickBasic or something later.
In QB, there are two kinds of subroutines: the old GOSUB..RETURN type and the
newer SUB..END SUG type.
The old syntax is:
GOSUB label
{execution will continue from here after return}
...
...
END ' of main program
label:
{subroutine body}
RETURN
Note that you _don't use the keyword SUB. All variables in a GOSUB subroutine
are global; the subroutine can see and alter any variable in the main program
or in another GOSUB - style subroutine. This is the original subroutine,
updated only to the extent of allowing a descriptive label instead of a line
number.
The newer type of subroutine is defined with
SUB name [parameter list]
subroutine body
END SUB
You call this kind of subroutine just by mentioning its name and (if it takes
paramaters) parameter list. Variables declared inside a SUB are invisible to
the rest of the program, unless declared SHARED.
For example, if you want to use a SUB to
sound the beeper a variable number of times, you could use:
SUB Beeper (Count%)
FOR I% = 1 TO Count%
BEEP
NEXT
END SUB
and the call
Beeper N%
anywhere in the program will produce N beeps.
QB keeps each SUB in its own editing window, so when you start editing the sub
the main program disappears. The F2 key brings up a the list of subs (and the
main program) so you can select the next one to edit. If you save the program
after adding a SUB, QB will automatically insert a DECLARE SUB line at the top
of the source file. This alerts the compiler to the presence of the sub; it
looks like this:
DECLARE SUB Beeper (Count%)
Details are in the online help.
--------------------------------------------------------------------------
--------------
If it's spam, it's a scam. Don't do business with Net abusers.