BASIC Programming/Subroutines and Functions

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 itself). 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.

Parameters

edit

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 player's 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 its data to manipulate other data that is returned. By Value is like making a carbon copy of a paper and then editing the carbon copy. By Reference sends the actual argument (parameter) to the function/subroutine, which is like editing the original copy of the paper. By Value is written as ByVal and By Reference is written as ByRef.

Functions

edit

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 function's 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 its 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.

GOSUB ... RETURN Statement

Purpose:

To branch to, and return from, a subroutine. Syntax:

GOSUB line number
.
.
.
RETURN [line number]

Comments:

line number is the first line number of the subroutine.

A subroutine may be called any number of times in a program, and a subroutine may be called from within another subroutine. Such nesting of subroutines is limited only by available memory.

A RETURN statement in a subroutine causes GW-BASIC to return to the statement following the most recent GOSUB statement. A subroutine can contain more than one RETURN statement, should logic dictate a RETURN at different points in the subroutine.

Subroutines can appear anywhere in the program, but must be readily distinguishable from the main program.

To prevent inadvertent entry, precede the subroutine by a STOP, END, or GOTO statement to direct program control around the subroutine. Examples:

10 GOSUB 40
20 PRINT "BACK FROM SUBROUTINE"
30 END
40 PRINT "SUBROUTINE";
50 PRINT " IN";
60 PRINT " PROGRESS"
70 RETURN
RUN
SUBROUTINE IN PROGRESS
BACK FROM SUBROUTINE

The END statement in line 30 prevents re-execution of the subroutine.

Conclusion

edit

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.