Chapter 7 Using Functions, Subs, and Modules

23
Chapter 7 Using Functions, Subs, and Modules

description

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. - PowerPoint PPT Presentation

Transcript of Chapter 7 Using Functions, Subs, and Modules

Page 1: Chapter 7 Using Functions, Subs, and Modules

Chapter 7Using Functions, Subs, and Modules

Page 2: 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

Page 3: Chapter 7 Using Functions, Subs, and Modules

Design for Expanded Vintage Videos Project

Page 4: Chapter 7 Using Functions, Subs, and Modules

Membership Management Form

Page 5: Chapter 7 Using Functions, Subs, and Modules

Modified Videos Form

Page 6: Chapter 7 Using Functions, Subs, and Modules

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

Page 7: Chapter 7 Using Functions, Subs, and Modules

Relationship between General

and Event Procedures

Procedures

Event procedures

Subprocedures

General procedures

Subprocedures

Functionprocedures

Page 8: Chapter 7 Using Functions, Subs, and Modules

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

Page 9: Chapter 7 Using Functions, Subs, and Modules

Primary Purposes of Subs and Functions

Function

argument

argument

argument

argument

value throughfunction name

argument

argument

argument

modifiedargument

argument

modifiedargument

processing

processing

Procedure

Page 10: Chapter 7 Using Functions, Subs, and Modules

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

Page 11: Chapter 7 Using Functions, Subs, and Modules

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

Page 12: Chapter 7 Using Functions, Subs, and Modules

Creating a Function

• The general form of the function definition statement is:

Function FuncName(parameter1 as type, parameter2 as type, …) as type

For exampleFunction 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.

Page 13: Chapter 7 Using Functions, Subs, and Modules

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

• ExampleSub Reverse(curFirst as Currency, curSecond as Currency)

Page 14: Chapter 7 Using Functions, Subs, and Modules

Relationship Between Sub Definition Statement and Statement Invoking the Sub

Page 15: Chapter 7 Using Functions, Subs, and Modules

Relationship Between Function Definition Statement and the Statement

Invoking the Function

Page 16: Chapter 7 Using Functions, Subs, and Modules

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

Page 17: Chapter 7 Using Functions, Subs, and Modules

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 SelectEnd Function

Page 18: Chapter 7 Using Functions, Subs, and Modules

VB Code Box 7-5Sub to Reverse Two Values

Sub Reverse(curFirst as Currency, curSecond as Currency)Dim curTemp as CurrencycurTemp = curFirstcurFirst = crSecondcurSecond = crTemp

End Sub

Page 19: Chapter 7 Using Functions, Subs, and Modules

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 LoopEnd Sub

Page 20: Chapter 7 Using Functions, Subs, and Modules

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 variablesPublic varName1 as type, varName2 as type, ...

Page 21: Chapter 7 Using Functions, Subs, and Modules

Scope of Global Variables

Form1

Form-level variables

Procedure

Local Variables

Procedure

Local Variables

Form2

Form-level variables

Procedure

Local Variables

Procedure

Local Variables

Project

Code module

Global variables

Page 22: Chapter 7 Using Functions, Subs, and Modules

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

Page 23: Chapter 7 Using Functions, Subs, and Modules

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