1 / 23

Chapter 7 Using Functions, Subs, and Modules

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.

art
Download Presentation

Chapter 7 Using Functions, Subs, and Modules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7Using Functions, Subs, and Modules

  2. 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

  3. Design for Expanded Vintage Videos Project

  4. Membership Management Form

  5. Modified Videos Form

  6. 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

  7. Relationship between General and Event Procedures

  8. 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

  9. Primary Purposes of Subs and Functions

  10. 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

  11. 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

  12. 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.

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

  14. Relationship Between Sub Definition Statement and Statement Invoking the Sub

  15. Relationship Between Function Definition Statement and the Statement Invoking the Function

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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, ...

  21. Scope of Global Variables

  22. 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

  23. 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

More Related