Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

BASIC Programming/Subroutines and Functions

From Wikibooks, open books for an open world
<BASIC Programming

Introduction

[edit |edit source]

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 justcalling a function/subroutine. There is hardly a difference between the two, except that a functionreturns a value, where a subroutine just repeats lines of code (A function is notalways 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 |edit source]

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 actuallychange 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 asByVal and By Reference is written asByRef.

Functions

[edit |edit source]

Ignore the line numbers; they are used for explaining the code.
Example 1 (FreeBASIC)

1.DeclareFunctionsalesTax(ByValsubTotalAsDouble)AsDouble2.DimSubTotal2AsDouble3.DimTotal2AsDouble4.SubTotal2=10.005.Total2=salesTax(SubTotal2)6.Print"Subtotal:";SubTotal27.Print"Total:";Total28.Sleep9.End10.11.FunctionsalesTax(ByValsubTotalAsDouble)AsDouble12.DimTotalAsDouble13.Total=subTotal*1.0714.returnTotal15.EndFunction



1. This line is the function'sprototype. Functions must be declared as a prototype before they are used, and must be defined after theend (of the program) statement. Function means that the following is related to functions. salesTax is the functionidentifier or its name, and in parentheses are theparameters (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 functionreturns).
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 youdefine the function, earlier youdeclared 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 variablescope).
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 whatline 5 receives, the function shoots out the variable Total.
15. Ends the function definition.

GOSUB...RETURNStatement

Purpose:

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

GOSUBlinenumber...RETURN[linenumber]

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:

10GOSUB4020PRINT"BACK FROM SUBROUTINE"30END40PRINT"SUBROUTINE";50PRINT" IN";60PRINT" PROGRESS"70RETURNRUNSUBROUTINEINPROGRESSBACKFROMSUBROUTINE

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

Conclusion

[edit |edit source]

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.

Retrieved from "https://en.wikibooks.org/w/index.php?title=BASIC_Programming/Subroutines_and_Functions&oldid=4352085"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp