230 likes | 301 Views
Chapter 7 Using Functions, Subs, and Modules. Plan for Revising Vintage Videos Application. Need a way of adding videos to system Need a better way of managing membership list Need capability to add late fees to customer’s bill Need a system to print alphabetical list of members or videos.
E N D
Plan for Revising Vintage Videos Application • Need a way of adding videos to system • Need a better way of managing membership list • Need capability to add late fees to customer’s bill • Need a system to print alphabetical list of members or videos
Using General Procedures • Event procedures are associated with a particular event and are not usually available to other forms • General procedures are used for specific tasks that are not associated with an event. • General procedures are defined in the General object of form and then invoked elsewhere in the project. • Two types of general procedures: • subs (subroutines) • functions
Subs and Functions • A sub is a unit of code that performs a specific tasks but returns no value • A function is similar to the built in functions in that arguments are passed to it and processed to compute a single value returned by its name
Working with General Procedures • General procedures must be created and then invoked • Invoking a function: variable = functionname(arg1, arg2, …, argn) • Invoking a sub: subname arg1, arg2, …, argn
Creating Subs and Functions • To create a sub or function you can • Use the Tools|Add Procedure menu command and select the type of procedure to add • or • simply type the word Sub or Function and press Enter after any existing event or general procedure • In either case, then add the parameters
Creating a Function • The general form of the function definition statement is: Function FuncName(parameter1 as type, parameter2 as type, …) as type For example Function intFindMax(intNum1 as Integer, intNum2 as Integer) as Integer • An important rule is that the name of the function must be assigned a value in the function.
Creating a Sub • The general form of the sub definition statement is: Sub SubName (parameter1 as type, parameter2 as type, …) • Note that the sub name is not assigned a type • Example Sub Reverse(curFirst as Currency, curSecond as Currency)
Relationship Between Sub Definition Statement and Statement Invoking the Sub
Relationship Between Function Definition Statement and the Statement Invoking the Function
Matching Arguments and Parameters • For both sub and functions, the number and type of arguments in invoking statement must match the number and type of parameters in the procedure definition statement • In both the argument and parameter list, fixed-size arrays are referenced by the name of the array followed by parentheses
VB Code Box 7-2Function to Compute Income Taxes Public Function curComputeTaxes(intNumExm As Integer, _ curGrossIncome As Currency) as Currency Dim curTaxIncome As Currency curTaxIncome = curGrossIncome - 4400 - intNumExm * 2800 Select Case curTaxIncome Case Is <= 26250 curComputeTaxes = 0.15 * TaxIncome Case Is <= 63550 curComputeTaxes = 3937.50 + 0.28 * (curTaxIncome - 26250) Case Is <= 132600 curComputeTaxes = 14385.50 + 0.31 * (curTaxIncome - 63550) Case Is < 288350 curComputeTaxes = 41170.50 + 0.36 * (curTaxIncome - 132600) Case Else curComputeTaxes = 86854.50 + 0.396 * (curTaxIncome - 288350) End Select End Function
VB Code Box 7-5Sub to Reverse Two Values • Sub Reverse(curFirst as Currency, curSecond as Currency) • Dim curTemp as Currency • curTemp = curFirst • curFirst = crSecond • curSecond = crTemp • End Sub
VB Code Box 7-6Sub to Sort Arrays Public Sub Sort(curList1() As Currency, strList2() _ As String, intNumList As Integer) Dim blnNoReversal As Boolean, intCounter As Integer blnNoReversal = False Do Until blnNoReversal blnNoReversal = True For intCounter = 0 To intNumList - 2 If curList1(intCounter) > curList1(intCounter + 1) Then Reverse curList1(intCounter),curList1(intCounter + 1) Reverse strList2(intCounter),strList2(intCounter+1) blnNoReversal = False End If Next Loop End Sub
Global Declarations and the Code Module • In a global declaration, the scope of global variables, as compared to form-level variables or procedure-level variables, includes all parts of the project. • The Code Module is the section of pure code that is known to all parts of the project. • Use Public statement to declare variables Public varName1 as type, varName2 as type, ...
Use of String Functions • Len(string)--returns number of characters in string • Left(string, N) or Right(string, N)--returns the leftmost or rightmost N characters in a string • Mid(String,P,N)--returns N characters in a string starting at Pth character • Ltrim(string) or Rtrim(String)--trims blank characters from left (right) end of string
Passing by Value in Subs • If there is a two-way communication between arguments and parameters, then you have passing by reference • If there is a one-way communication between arguments and parameters, then you have passing by value • It is possible to force passing by value by adding the keyword ByVal prior to a variable in the procedure definition statement