How to Call a Function in QBasic

How to Call a Function in QBasic thumbnail
Learning to program a computer can prove challenging; QBasic is a good introductory language.

QBasic, also known as Quick Basic, is a programming language developed by Microsoft from BASIC. Due to its easy learning curve, it commonly serves as an introductory programming language. The language allows both subroutines and functions to help organize the program into easy-to-understand logic segments. In any programming language, the term to call a function refers to the ability to cause your program to run a specific segment of code predefined in the program. Your program will use a specific function name to tell the program which function to run when it is called.

Instructions

    • 1

      Describe the parameters your function needs. If you write a function to add two numbers, the function will require two parameters --- Number1 and Number2 --- in order to know what to add. A parameter is an outside piece of knowledge the function requires to perform the logic. If a function is meant to add two numbers, the function must know those two external numbers to perform the "add" logic. Thus, the function will require two parameters: the two numbers to add.

    • 2

      Program the return value of the function. (A return value is the resulting answer a function produces.) The number that results from the add function described in Step 1 is the return value. By definition in QBasic, a function must return a value and cannot have a return value of void, as some other languages allow. A void function in other languages has no return value. A void function in QBasic is defined instead as a subroutine. If the return value is a string, meaning it is non-numerical in programming terms, you must add a "$" symbol to the end of the function name to indicate the return value is not a numerical value and instead a string value. A numerical return value does not need any additional symbols.

    • 3

      Declare the function. Using the keyword "FUNCTION," declare the function in the code. Declaration of a function for adding two numbers occurs as follows:

      FUNCTION Sum(Number1, Number2)

      Sum = Number1 + Number2

      END FUNCTION

      The value returned must have the same name as the function, which is how QBasic defines the return value.

    • 4

      Call the function from the main code. To call a function within the code of a QBasic program, simply type the name of the function with the appropriate parameters passed in. Since all functions in QBasic have a return type, the function call must accompany an action, such as an assignment or a PRINT. For instance, "PRINT Sum(1, 2)" will output the value 3 to the screen when you run the program.

    • 5

      Check to verify the function is declared before attempting to use it. QBasic will create a function declaration for you, although it may not always fall logically in your desired program where you want it to. This function declaration must occur prior to where the function is called or defined in the program.

Tips & Warnings

  • Type all QBasic keywords in the program in all capital letters, such as "FUNCTION." You can name the variables as you see fit; you do not necessarily need to name them "Sum," "Number1" or "Number2."

Related Searches:

References

Resources

  • Photo Credit Stockbyte/Stockbyte/Getty Images

Comments

You May Also Like

  • Basic Math Functions

    Much of the materials in algebra 1 and algebra 2 courses revolve around basic math functions. When you draw a graph on...

Related Ads

Featured