Beginner's Programming Tutorial in QBasic

(Kiana) #1

Subroutines and functions


A subroutine (also called a "module") is a "mini-program" inside your program. In other words, it
is a collection of commands--and can be executed anywhere in your program.
To create a subroutine:



  1. Go to the "Edit" menu

  2. Select "New Sub"

  3. Enter a name for the subroutine

  4. Type a list of commands between SUB and END SUB


To use the subroutine:



  1. Press F2

  2. Select "Untitled"

  3. Press Enter to return to the "main module"

  4. Use CALL to execute the subroutine


TIP: Another way to create a subroutine is by
typing SUB <name> in the main module.
SUB MySub

The following example does not use subroutines:
PRINT "Enter some text:";
INPUT text$PRINT "The text you entered was: "; text$
PRINT "Enter some text:";INPUT text$
PRINT "The text you entered was: "; text$
PRINT "Enter some text:";INPUT text$
PRINT "The text you entered was: "; text$
PRINT "Enter some text:";
INPUT text$PRINT "The text you entered was: "; text$
PRINT "Enter some text:";INPUT text$
PRINT "The text you entered was: "; text$

Free download pdf