Subroutines, Functions, SHARED, GOSUB and GOTO


This tutorial is about creating/calling subroutines, subsroutines,
line numbers/labels, creating/using functions and functions.

  • Avoid using GOTO

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 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 1
2 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
 LOOP
1 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

 

 
  • COMMON and SHARED

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.

 
  • DECLARE SUB amd DECLARE FUNCTION

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)

  • DEF FN, END DEF amd EXIT DEF

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 DEF

Use the EXIT DEF statement to leave
a function made using DEF FN.

A function is called like this:
fnfunction name

A function must be in the code that
comes before the code using it.

  • FUNCTION, END FUNCTION and EXIT FUNCTION

Use the FUNCTION statement to create functions.
This is how FUNCTION and END FUNCTION are used:


FUNCTION function name
 code
END FUNCTION

or:

FUNCTION function name (variable list)
 code
END FUNCTION

Use the EXIT FUNCTION statement to leave a function.

A function is the same as a subroutine but
a function also returns a value.

DECLARE FUNCTION Test ()
CLS
PRINT Test
FUNCTION Test
Test = 100
END FUNCTION
 
 
  • GOSUB and RETURN

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

 
  • GOTO

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".

 
  • Line Numbers and Labels

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.

GOTO Test
END
Test:
 
  • ON ... GOTO and ON ... GOSUB

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 number2

This is how the ON ... GOSUB statement is used:
ON number GOSUB label1/line number1, label2/line number2

 

 
  • SHARED

The SHARED statement indicates that variables
are shared with the subroutines where the SHARED
statement is located and the main routine only.

 
  • STATIC

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.

  • SUB, END SUB, CALL and EXIT 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 SUB

or:

SUB sub name (variable list)
 code
END SUB

Use 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 list

Use the EXIT SUB statement to leave a subroutine.

 
  • Using (ON ...) GOTO/GOSUB, RESUME and RETURN in Subroutines and Functions:

There are some rules for using the following
statements in subroutines and Functions:

GOSUB
GOTO
ON (...) GOTO/GOSUB
RESUME
RETURN
RUN

See What Subroutines and Functions Are
for information about the rules.

  • Variable Lists

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:

  1. A subroutine made using a label/line number, GOSUB and
    RETURN, these can be placed in both types of subroutines.
  2. A subroutine made using the SUB, END SUB, EXIT SUB,
    CALL and DECLARE statements.
    There are some rules for this type of subroutines:
    1. 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.
    2. A label/line number must be in the same
      subroutine/function as the GOTO, GOSUB, RESUME, RETURN
      or RUN statement referring to it.
    3. A Label/line number referred to by
      a ON ... GOSUB/GOTO statement
      must in the program's main routine
    4. The same labels/line numbers cannot be used more than once,
      even if they are in different subroutines/functions.
    5. 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.
  3. There are two types of FUNCTIONS:
    1. A function made using DEF FN and END DEF.
    2. A function made using  the FUNCTION
      END FUNCTION, EXIT FUNCTION
      and DECLARE statements.
       
  4. 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.

 

QBasic For All 2000
Created  16 September 2000
last edited 25 Febuary 2002