BASIC Programming/Subroutines and Functions
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Introduction
Functions and Subroutines are lines of code that you use more than once. The purpose of functions and subroutines is to save time and space by just calling a function/subroutine. There is hardly a difference between the two, except that a function returns a value, where a subroutine just repeats lines of code (A function is not always used more than once).
An example of a function, say you are making a program that calculates sales tax. The function would ask for a subtotal, take that number and multiply it by 1.07 (If sales tax is 7%, the 1 is because you are adding onto the subtotal to make a total, not finding the sales tax it self). The function would take the new total and give it back to the program.
A subroutine would be code that is reused, like a shop in a game. Every time the user goes to the shop, the program will go back to the Shops Subroutine, and if the user bought an item, it would call a "Buy Item" function.
[edit] Parameters
Parameters are used in functions and subroutines. They pass data to be used with the function/parameter. For use with our two examples:
Function Parameters) The Subtotal passed to the function is a parameter.
Subroutine Parameters) The players amount of gold could be sent to the subroutine so the shop
knows how much money the player can spend.
There are two different ways to send parameters, by the value or by reference. By Value means that the function can not actually change the data outside of the function, but can use it's data to manipulate other data that is returned. By Value is like making a carbon copy of a paper, and editing the carbon copy. By Reference sends the the actual argument (parameter) to the function/subroutine, which is like editing the original copy of the paper. By Value is wrote as ByVal and By Reference is wrote as ByRef.
[edit] Functions
Ignore the line numbers, they are used for explaining the code.
Example 1 (FreeBASIC)
1. Declare Function salesTax(ByVal subTotal As Double) As Double 2. Dim SubTotal2 As Double 3. Dim Total2 As Double 4. SubTotal2 = 10.00 5. Total2 = salesTax(SubTotal2) 6. Print "Subtotal:" ; SubTotal2 7. Print "Total:" ; Total2 8. Sleep 9. End 10. 11. Function salesTax(ByVal subTotal As Double) As Double 12. Dim Total As Double 13. Total = subTotal*1.07 14. return Total 15. End Function
1. This line is the functions prototype. Functions must be declared as a prototype before they are used, and must be defined after the end(of the program) statement. Function means that the following is related to functions. salesTax is the function identifier or it's name, and in parentheses are the parameters (If you have more than one parameter, separate them with a comma). After the parentheses tells what Data Type the function returns. Double is a Data Type that signifies a number with decimals (00.00).
2. Create the SubTotal2 variable (Parameters can not be the same as existing identifiers).
3. Create the Total2 variable.
4. Define SubTotal2 with the value 10.00 (Change this to get new outputs)
5. Define Total2 by passing SubTotal2 as an argument(parameter) to the function salesTax (The value of Total2 will be what the function returns).
6. Display the subtotal.
7. Display the total.
8. Wait for the user to press enter (So you can read the output with out it flashing away in a second).
9. End the program (In a sense. You can't interact with the user past the End point)
10. Blank Line, easier to read the code.
11. This is where you define the function, earlier you declared the function.
12. Create the variable Total (This variable can only be used in the function, because it was defined in the function. This is called the variable scope).
13. You do not need to declare subTotal, because it was defined as a parameter, this math finds the total with sales tax and assigns it to the variable Total.
14. This is what line 5 receives, the function shoots out the variable Total.
15. Ends the function definition.
[edit] Subroutines
[edit] Conclusion
Subroutines do not return any values, while functions return values. Subroutines are also allowed to change values of the parameters while functions are supposed to maintain.