Hardware Reference
In-Depth Information
On exit from the subroutine, the value of c% will have been modified to 1
greater than the value of lim% immediately prior to the subroutine call.
Since line numbers are not required using QuickBASIC (in common with
later versions of Microsoft BASIC for DOS as well as virtually all modern
BASIC compilers) a label is used (instead of a line number) to mark the start
of the delay subroutine, as shown below:
REM Start of main program
...
...
GOSUB Delay
...
...
GOSUB Delay
...
...
GOSUB Delay
...
...
END
REM Delay subroutine
Delay:
FORc%=0TO10000: NEXT c%
RETURN
It is important to note that the label ( Delay ) is immediately followed by a colon
and that the main body of program code must be terminated by an END statement
in order to prevent execution of the subroutine when the end of the code has
been reached. If this should ever happen, an error condition will result as the
RETURN statement does not have a matching GOSUB .
Procedures
A user-defined procedure can be thought of as a named subroutine. The pro-
cedure is simply CALL ed by name rather than by GOSUB followed by a label.
This can be instrumental in not only making the resulting code more readable
but it also ensures that the structure of the program can be easily understood.
A further advantage of procedures is that parameters may be passed into pro-
cedures and values returned to the main program. Variables which are to be
common with the main program may be declared at the start of the procedure
using the SHARED statement (otherwise all variables internal to the procedure
will be strictly local).
Procedures are defined using statement of the form SUB < name > and are
terminated by END SUB . Procedures may also contain references to other pro-
cedures (i.e. procedures can be 'nested'). Procedure names should be chosen so
they do not conflict with any variable names nor should they be BASIC reserved
words.
The previous delay subroutine can be easily written as a procedure:
REM Delay procedure
SUB Delay(lim%)
FORc%=0TOlim%: NEXT c%
END SUB
Search WWH ::




Custom Search