Subroutines, Functions, SHARED, GOSUB and GOTO
This tutorial is about creating/calling subroutines, subsroutines,
line numbers/labels, creating/using functions and functions.
|
GOTO seems a useful statement. The program
can be sent to every part of the code as
long as the code is not outside the subroutines/
function where the GOTO statement is located.
Using GOTO too much can make the code
very hard to read, each time a GOTO statement
is found, the whole program has to be checked
for the label or line number to which the GOTO
statement referred and in some cases the code
can even be made shorter when not using GOTO.
It is better to use statements such as:
GOSUB and RETURN
SUB and CALL
DO and LOOP
FOR and NEXT.
To leave a loop or subroutines EXIT DO, EXIT FOR
and EXIT SUB can be used.
The examples show how using GOTO can be avoided.
'This program keeps asking for input until nothing is entered.CLS 1 LINE INPUT "Enter a text: "; Text$ IF Text$ = "" THEN GOTO 2 GOTO 1 2 END'The code above can be replaced by this code:CLS DO LINE INPUT "Enter a text: "; Text$ IF Text$ = "" THEN EXIT DO LOOP END
'This is a program which displays the line "This is an example." 10 times.Number = 0 1 Number = Number + 1 PRINT "This is an example. IF Number = 10 THEN GOTO 2 GOTO 1 2 END'This program does exactly the same as the program above.FOR Number = 1 TO 10 PRINT "This is an example." NEXT Number END
'This program asks a question, then checks if the answer is correct. INPUT " 1 + 1 = ", Answer IF Answer = 2 THEN GOTO 1 ELSE GOTO 2 3 END 1 PRINT "Right." GOTO 3 2 PRINT "Wrong the answer was 2." GOTO 3 'This program does exactly the same as the program above. INPUT " 1 + 1 = ", Answer IF Answer = 2 THEN PRINT "Right." ELSE PRINT "Wrong the answer was 2." END IF END |
'This program increases a's value 'by 1 each loop until a's value is 100.a = 0 CLS 1 LOCATE 1, 1 PRINT a GOTO 2 3 GOTO 12 IF a = 100 THEN GOTO 4 a = a + 1 GOTO 3 4 END'Does the same as the program above but 'it uses DO and LOOP and GOSUB instead of GOTO.a = 0 CLS DO LOCATE 1, 1 PRINT a GOSUB 1 LOOP1 IF a = 100 THEN END a = a + 1 RETURN'An even better way of doing what the two programs above do is:CLS FOR a = 0 TO 100 LOCATE 1, 1 PRINT a NEXT a END
'This example shows an object moving from left to right on the screen. x = 1 CLS 1 LOCATE 5, x PRINT "*" FOR sd = 0 TO 2500: NEXT sd 'Slow down the program. LOCATE 5, x PRINT " " GOTO 2 3 GOTO 1 2 IF x = 80 THEN END x = x + 1 GOTO 3 'This program uses CALL and DO and LOOP instead of GOTO but does the same 'as the program above. DECLARE SUB Move () COMMON SHARED x x = 1 CLS DO LOCATE 5, x PRINT "*" FOR sd = 0 TO 2500: NEXT sd LOCATE 5, x PRINT " " CALL Move LOOP SUB Move IF x = 80 THEN END x = x + 1 END SUB |
|
Use the COMMON statement to share variables
between two Quick Basic programs. The program
with which the variables are shared should
be executed using CHAIN otherwise the variables
will not be shared.The SHARED keyword is used in combination with
DIM and COMMON to make variables shared
with all subroutines in a program.
Example 1: COMMON a, b, c$ Example 2: COMMON SHARED a, b, c$
Example 3: DIM SHARED a(100) Example 4: DIM a(100) COMMON SHARED a()
|
Use the DECLARE SUB statement to tell that
there is a subroutine and what variables
are shared with the subroutines.
This is how DECLARE SUB is used:
DECLARE SUB subroutine's name (variable list)Use the DECLARE FUNCTION statement to tell that there is a
function and what variables are shared with the function.
This is how DECLARE FUNCTION is used:
DECLARE SUB function name (variable list)
|
Another way to create functions, these
functions can only be made in and called
from the program's main routine.
This is how the DEF FN statement is used:
DEF fnfunction name
code
END DEFUse the EXIT DEF statement to leave
a function made using DEF FN.A function is called like this:
fnfunction nameA function must be in the code that
comes before the code using it.
Example 1:
DEF fnTest BEEP END DEFPRINT fnTest 'Call the function by using it in a PRINT statement.
|
Use the FUNCTION statement to create functions.
This is how FUNCTION and END FUNCTION are used:
FUNCTION function name
code
END FUNCTIONor:
FUNCTION function name (variable list)
code
END FUNCTIONUse the EXIT FUNCTION statement to leave a function.
A function is the same as a subroutine but
a function also returns a value.
- Example 1:
DECLARE FUNCTION Test () CLS PRINT TestFUNCTION Test Test = 100 END FUNCTION
- Example 2:
DECLARE FUNCTION DivideByTwo (Value)INPUT "Enter a number: ", Value PRINT DivideByTwo(Value)FUNCTION DivideByTwo (Value) DivideByTwo = Value / 2 END FUNCTION
- Example 3:
DECLARE FUNCTION FirstChr$ (Text$)LINE INPUT "Enter a text: ", Text$ PRINT FirstChr$(Text$)FUNCTION FirstChr$ (Text$) FirstChr$ = LEFT$(Text$, 1) END FUNCTION
|
Use the GOSUB and RETURN statements to create subroutines.
RETURN must be used at the point at which the subroutine
is left. RETURN basically does the same as EXIT SUB.
RETURN will send a program to the statement that comes
next to the GOSUB statement that called the subroutine.
To make RETURN go to a line number/label after a subroutine
has been left use RETURN like this:
RETURN line number/label
- Example 1:
CLS PRINT "In main in part of program." GOSUB Sub1 'Go to Sub1. ENDSub1: PRINT "In subroutine of program." RETURN
- Example 2:
CLS PRINT "In main routine of program." GOSUB Sub1END 1 ENDSub1: PRINT "In subroutine of program." RETURN 1
|
Use the GOTO statement to go to a label or line number.
You should try to avoid using GOTO as much as you can.
See "Avoid Using Goto for more information".
- Example:
GOTO 1 Endprog: END1 GOTO Endprog
|
A line number is a number that can be put on the left part of a line. Two line numbers cannot have the same number. A label can have numbers and letters in it and must end with a : character. Labels and line numbers can be used to send a program to using a statement such as GOTO and the program will continue running from the point where the label or line number is located.
- Example:
GOTO Test END Test:
|
Use the ON ... GOTO and ON ... GOSUB statements
to let GOTO or GOSUB select a label or line number
based on the value of a number.
This is how the ON ... GOTO statement is used:
ON number GOTO label1/line number1, label2/line number2This is how the ON ... GOSUB statement is used:
ON number GOSUB label1/line number1, label2/line number2
- Example 1:
CLS INPUT "Enter a value: ", Value ON Value GOTO One, Two, Three ENDOne: PRINT "The number one has been entered." ENDTwo: PRINT "The number two has been entered." ENDThree: PRINT "The number three has been entered." END
- Example 2:
CLS PRINT " 1 = yellow" PRINT " 2 = orange" PRINT " 3 = red" PRINT " 4 = green" PRINT " 5 = blue" PRINT INPUT "Select a color: ", Value PRINT PRINT "The color "; ON Value GOSUB 1, 2, 3, 4, 5 PRINT " has been chosen." END1 PRINT "yellow" RETURN2 PRINT "orange" RETURN3 PRINT "red" RETURN4 PRINT "green" RETURN5 PRINT "blue" RETURN
|
The SHARED statement indicates that variables
are shared with the subroutines where the SHARED
statement is located and the main routine only.
- Example 1:
DIM b(10)SUB Test SHARED a, b() END SUB
|
STATIC makes sure a variable's value stays the same
for one subroutine even if another subroutine changes it.
Meaning if one subroutine uses a variable and another
subroutine changes the variable's value the variable
will not have changed for the subroutine using STATIC.
- Example:
CLSSUB Test STATIC a a = 100 CALL Test2 PRINT a END SUBSUB Test2 a = 200 PRINT a 'Returns 200. END SUB
|
Use the SUB statement to tell where a subroutine starts and
use the END SUB statement to tell where a subroutine ends.
This is how SUB and END SUB are used:
SUB sub name
code
END SUBor:
SUB sub name (variable list)
code
END SUBUse the CALL statement to go to a sub.
This is how CALL is used:
CALL subroutine's name (variable list)A subroutine can also be called like this:
subroutine's name variable listUse the EXIT SUB statement to leave a subroutine.
- Example 1:
DECLARE SUB Test ()CALL TestSUB Test BEEP END SUB
- Example 2:
DECLARE SUB BeepTwice ()BeepTwiceSUB BeepTwice () BEEP BEEP END SUB
|
There are some rules for using the following
statements in subroutines and Functions:GOSUB GOTO ON (...) GOTO/GOSUB RESUME RETURN RUNSee What Subroutines and Functions Are
for information about the rules.
|
A variable list is a list of variables
next to a CALL or DECLARE statement or a name of a
subroutine or function. Variables are separated
by commas. A variable's type is defined by its
suffix or using AS INTEGER, AS LONG, AS DOUBLE, AS SINGLE
or AS STRING following the variable's name.Variable lists are always put between brackets except when
a subroutine is called and there is no CALL statement,
no brackets are used.Use empty brackets following the name
of an array to indicate it is an array.
A subroutine is a piece of code which has
to be called to run it. It can be called
from any point in a program. There are two
types of subroutines:
- A subroutine made using a label/line number, GOSUB and
RETURN, these can be placed in both types of subroutines.- A subroutine made using the SUB, END SUB, EXIT SUB,
CALL and DECLARE statements.
There are some rules for this type of subroutines:
- Before variables from one subroutine can be
used in another subroutine the variables must
be shared or placed in a list of variables
at the end of the CALL and DECLARE statement
referring to the subroutine, to be sent to that subroutine.- A label/line number must be in the same
subroutine/function as the GOTO, GOSUB, RESUME, RETURN
or RUN statement referring to it.- A Label/line number referred to by
a ON ... GOSUB/GOTO statement
must in the program's main routine- The same labels/line numbers cannot be used more than once,
even if they are in different subroutines/functions.- RETURN label/line number does not work
in a subroutine or function.
A function is the same as a subroutine except
it returns a value, unlike a subroutine.- There are two types of FUNCTIONS:
- A function made using DEF FN and END DEF.
- A function made using the FUNCTION
END FUNCTION, EXIT FUNCTION
and DECLARE statements.
- The same rules for variables,arrays, labels and
line numbers as for subroutines go for functions.The main routine of a program is the part of the
program outside the subroutines and functions.
It is also the part where the program execution
begins.
- Example:
'_ CLS ' |____Main routine. CALL Example: '_|SUB Example END SUB
QBasic For All 2000
Created 16 September 2000
last edited 25 Febuary 2002