Home Previous section Next section

Subroutines and functions Star


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$

    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$

By using a subroutine, the above program can be simplified like this:

    CALL GetText
    CALL GetText
    CALL GetText
    CALL GetText
    CALL GetText
    CALL GetText
    CALL GetText


    SUB GetText

      PRINT "Enter some text:";
      INPUT text$
      PRINT "The text you entered was: "; text$

    END SUB

The following is even more concise:
    FOR x = 1 TO 7
      CALL GetText
    NEXT


    SUB GetText
      PRINT "Enter some text:";
      INPUT text$
      PRINT "The text you entered was: "; text$
    END SUB

Parameters

Parameters are numbers and strings that you pass to a subroutine, much like a QBasic command.
    ' This passes 16 as a parameter:

    CALL OutputNumber(16)


    ' Notice the parentheses around the parameter "num."
    ' Any variables placed inside the parentheses are set as
    ' the subroutine's parameters.

    SUB OutputNumber (num)

      PRINT num

    END SUB
Output:
    16

TIP: Variables created in your program cannot be used in the subroutines unless you use COMMON SHARED (followed by a variable) in the main module.

COMMON SHARED x$



Functions

A function is the same as a subroutine, except it returns a value. Also, you must leave out the CALL command.

To return a value, set a variable with the same name as the function.

    PRINT Add(10, 7)


    FUNCTION Add (num1, num2)

      Add = num1 + num2

    END FUNCTION
Output:
    17

Since a function can return a value, the name of the function can end with special characters (see Variable types, Using special characters).

    ' Notice the dollar sign ($) after "Add."  It means
    ' the function returns a string.

    PRINT Add$("Hello", "World")


    FUNCTION Add$ (str1$, str2$)

      Add$ = str1$ + str2$

    END FUNCTION
Output:
    HelloWorld

Next section


Feel free to distribute this tutorial, upload it to your website, link to it from your site, etc.

http://www.geocities.com/progsharehouse/qbtutor
Mirror: http://development.freeservers.com/qbtutor