chapter 7: sub and function procedures

41
Chapter 7: Sub and Function Procedures Programming with Microsoft Visual Basic 2005, Third Edition

Upload: cassidy-jimenez

Post on 31-Dec-2015

35 views

Category:

Documents


3 download

DESCRIPTION

Chapter 7: Sub and Function Procedures. Programming with Microsoft Visual Basic 2005, Third Edition. Creating Sub and Function Procedures Lesson A Objectives. Explain the difference between a Sub procedure and a Function procedure Create a procedure that receives information passed to it - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 7: Sub and Function Procedures

Chapter 7: Sub and Function Procedures

Programming with Microsoft Visual Basic 2005, Third Edition

Page 2: Chapter 7: Sub and Function Procedures

2Programming with Microsoft Visual Basic 2005, Third Edition

Creating Sub and Function Procedures

Lesson A Objectives• Explain the difference between a Sub procedure

and a Function procedure

• Create a procedure that receives information passed to it

• Explain the difference between passing data by value and passing data by reference

• Create a Function procedure

Page 3: Chapter 7: Sub and Function Procedures

3Programming with Microsoft Visual Basic 2005, Third Edition

Previewing the Completed Application

• Go to Run command on Windows Start menu

• Browse to the VB2005\Chap07 folder

• Open the Harvey Industries.exe file

• The Harvey Industries user interface appears

Page 4: Chapter 7: Sub and Function Procedures

4Programming with Microsoft Visual Basic 2005, Third Edition

Previewing the Completed Application (continued)

Figure 7-1: Payroll amounts shown in the user interface

Page 5: Chapter 7: Sub and Function Procedures

5Programming with Microsoft Visual Basic 2005, Third Edition

Procedures

• Procedure

– Block of program code that performs a specific task

• Two types of procedures in Visual Basic

– Sub procedure: does not return a value

– Function procedure: does return a value

Page 6: Chapter 7: Sub and Function Procedures

6Programming with Microsoft Visual Basic 2005, Third Edition

Sub Procedures

• Event procedure

– Associated with a specific object and event

– Called by Visual Basic in response to an event

• Independent procedure

– Independent of any object and event

– Invoked from code using a Call statement

• Parameter: data passed to procedure at call time

Page 7: Chapter 7: Sub and Function Procedures

7Programming with Microsoft Visual Basic 2005, Third Edition

Passing Information to an Independent Sub Procedure

• Call syntax: Call procedurename([argumentlist])– argumentlist: used to pass information (optional)

• Argument: data item in an argumentlist

• Parameter: data item in a parameterlist

• Relationship between arguments and parameters– Should agree in number, position, and data type

• Types of arguments passed to a procedure– Variable, literal constant, named constant, keyword

Page 8: Chapter 7: Sub and Function Procedures

8Programming with Microsoft Visual Basic 2005, Third Edition

Passing Variables

• Variables specified by name, address, and value

• Passing by value

– Passes a copy of the data stored in a variable

• Passing by reference

– Passes the memory address of a variable

– Allows a procedure to change contents of variable

Page 9: Chapter 7: Sub and Function Procedures

9Programming with Microsoft Visual Basic 2005, Third Edition

Passing by Value

• Copy of data, not variable address, is passed

• How to pass by value– Include the keyword ByVal before a parameter

• Reasons to pass by value– Procedure needs to know contents of variable– Procedure does not need to change original value

• By default, Visual Basic passes by value

Page 10: Chapter 7: Sub and Function Procedures

10Programming with Microsoft Visual Basic 2005, Third Edition

Passing By Value (continued)

Figure 7-4: Code for the Pet Information application (continued)

Page 11: Chapter 7: Sub and Function Procedures

11Programming with Microsoft Visual Basic 2005, Third Edition

Passing By Value (continued)

Figure 7-4: Code for the Pet Information application

Page 12: Chapter 7: Sub and Function Procedures

12Programming with Microsoft Visual Basic 2005, Third Edition

Passing By Reference

• Variable address (memory location) is passed

• Receiving procedure can access variable

• Reason to pass by reference– Procedure needs to change a variable’s contents

• How to pass by reference– Include keyword ByRef before a parameter

Page 13: Chapter 7: Sub and Function Procedures

13Programming with Microsoft Visual Basic 2005, Third Edition

Passing By Reference (continued)

Figure 7-8: Code for the Gross Pay application (continued)

Page 14: Chapter 7: Sub and Function Procedures

14Programming with Microsoft Visual Basic 2005, Third Edition

Passing By Reference (continued)

Figure 7-8: Code for the Gross Pay application (continued)

Page 15: Chapter 7: Sub and Function Procedures

15Programming with Microsoft Visual Basic 2005, Third Edition

Passing By Reference (continued)

Figure 7-8: Code for the Gross Pay application

Page 16: Chapter 7: Sub and Function Procedures

16Programming with Microsoft Visual Basic 2005, Third Edition

Function Procedures

• Function procedure

– A block of code that performs a specific task

– Returns a value after completing task

• Examples of built-in functions: Val and InputBox

• Create your own functions using syntax template

– As datatype in header indicates return type of data

– Return expression type must agree with As datatype

Page 17: Chapter 7: Sub and Function Procedures

17Programming with Microsoft Visual Basic 2005, Third Edition

Function Procedures (continued)

Figure 7-11: Syntax and an example of a Function procedure

Page 18: Chapter 7: Sub and Function Procedures

18Programming with Microsoft Visual Basic 2005, Third Edition

The Pine Lodge Application

• Objective: calculate employee’s new pay

• Application uses a function to calculate new pay

• Function requirements– Input: employee’s current hourly pay and raise rate– Process: calculate raise and then add to current pay– Output: return new pay to calling procedure

Page 19: Chapter 7: Sub and Function Procedures

19Programming with Microsoft Visual Basic 2005, Third Edition

The Pine Lodge Application (continued)

Figure 7-12: Sample run of the Pine Lodge application

Page 20: Chapter 7: Sub and Function Procedures

20Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson A

• Two types of procedures: event and independent

• Function: performs a task and returns a value

• Independent procedures and functions are called from the application’s code

• Pass by value: send a copy of variable contents to a procedure or function

• Pass by reference: send a variable address to a procedure or function

Page 21: Chapter 7: Sub and Function Procedures

21Programming with Microsoft Visual Basic 2005, Third Edition

Coding The Harvey Industries PayrollApplication

Lesson B Objectives

• Add a combo box to a form

• Add items to a combo box

• Sort the contents of a combo box

• Select a combo box item from code

Page 22: Chapter 7: Sub and Function Procedures

22Programming with Microsoft Visual Basic 2005, Third Edition

Coding The Harvey Industries PayrollApplication

Lesson B Objectives (continued)

• Determine the current item in a combo box

• Round a number

• Code a combo box’s TextChanged event procedure

Page 23: Chapter 7: Sub and Function Procedures

23Programming with Microsoft Visual Basic 2005, Third Edition

Harvey Industries

• Objective: calculate and display pay information

• Components of employee pay– Employee’s weekly gross pay– Social Security tax – Medicare (FICA) tax– Federal withholding tax (FWT)– Net pay

• Input: name, marital status, hours, rate, allowances

Page 24: Chapter 7: Sub and Function Procedures

24Programming with Microsoft Visual Basic 2005, Third Edition

Harvey Industries (continued)

Figure 7-15: Partially completed user interface for the Harvey Industries application

Page 25: Chapter 7: Sub and Function Procedures

25Programming with Microsoft Visual Basic 2005, Third Edition

Including a Combo Box in an Interface

• Combo box

– Allows user to select from a number of choices

– Allows user to type an entry not on list

– Can save a space on a form

• List box does not share features two and three

• DropDownStyle property

– Values: Simple, DropDown (default), DropDownList

Page 26: Chapter 7: Sub and Function Procedures

26Programming with Microsoft Visual Basic 2005, Third Edition

Including a Combo Box in an Interface (continued)

Figure 7-16: Examples of the combo box styles

Page 27: Chapter 7: Sub and Function Procedures

27Programming with Microsoft Visual Basic 2005, Third Edition

Including a Combo Box in an Interface (continued)

• Change in item value causes TextChanged event

• Use Item collection’s Add method to add an item

• Other properties of a combo box– Sorted: sorts items in dictionary order– SelectedIndex: used to select an item in list portion– SelectedItem: determines which item is selected– Text: used to get or set a value in text portion

Page 28: Chapter 7: Sub and Function Procedures

28Programming with Microsoft Visual Basic 2005, Third Edition

Including a Combo Box in an Interface (continued)

Figure 7-17: Code corresponding to the combo boxes shown in Figure 7-16

Page 29: Chapter 7: Sub and Function Procedures

29Programming with Microsoft Visual Basic 2005, Third Edition

Coding the xCalcButton’s Click Event Procedure

• Review pay components to calculate and display:– Gross pay– FWT (federal withholding tax)– FICA tax– Net pay

• Procedure will call the GetFwt function

Page 30: Chapter 7: Sub and Function Procedures

30Programming with Microsoft Visual Basic 2005, Third Edition

Coding the xCalcButton’s Click Event Procedure (continued)

Figure 7-22: Pseudocode for the xCalcButton’s Click event procedure

Page 31: Chapter 7: Sub and Function Procedures

31Programming with Microsoft Visual Basic 2005, Third Edition

Coding the GetFwt Function

• How to calculate weekly taxable wages

– Multiply number of withholding allowances by $63.46

– Subtract result in first step from weekly wages

• Determining federal withholding tax (FWT)

– Evaluate weekly taxable wages and filing status

– Use data to look up FWT in special FWT tables

• GetFwt function emulates FWT table lookup

Page 32: Chapter 7: Sub and Function Procedures

32Programming with Microsoft Visual Basic 2005, Third Edition

Coding the GetFwt Function (continued)

Figure 7-25: Weekly FWT tables (Single person)

Page 33: Chapter 7: Sub and Function Procedures

33Programming with Microsoft Visual Basic 2005, Third Edition

Coding the GetFwt Function (continued)

Figure 7-30: FWT calculations for Single taxpayers

Page 34: Chapter 7: Sub and Function Procedures

34Programming with Microsoft Visual Basic 2005, Third Edition

Completing the xCalcButton’s Click Event Procedure

• Call GetFwt function from Click event procedure

• Three values passed to GetFwt– status variable contents– allowances variable contents– gross variable contents

• Value returned by GetFwt assigned to fwt variable

• Use Math.Round function to round three values– Gross pay, FWT, and FICA tax amounts

Page 35: Chapter 7: Sub and Function Procedures

35Programming with Microsoft Visual Basic 2005, Third Edition

Completing the xCalcButton’s Click Event Procedure (continued)

Figure 7-34: Payroll calculations displayed in the interface

Page 36: Chapter 7: Sub and Function Procedures

36Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson B

• Combo box displays a list of items for selection

• Combo box allows user to type entry not on list

• Specify style of combo box using DropDownStyle property

• Use Items collection’s Add method to add items to a Combo box

• Math.Round function rounds off values to arbitrary number of decimal places

Page 37: Chapter 7: Sub and Function Procedures

37Programming with Microsoft Visual Basic 2005, Third Edition

Completing the Harvey Industries Payroll Application

Lesson C Objectives• Prevent a form from closing

Page 38: Chapter 7: Sub and Function Procedures

38Programming with Microsoft Visual Basic 2005, Third Edition

Coding The MainForm’sFormClosing Event Procedure

• FormClosing event– Occurs when a form is about to be closed

• Two ways to cause a FormClosing event– Computer processes the Me.Close() statement – User clicks the Close button on the form’s title bar

• Requirement for FormClosing event procedure – Verifying that user wants to close the application– Taking appropriate action based on user’s response

Page 39: Chapter 7: Sub and Function Procedures

39Programming with Microsoft Visual Basic 2005, Third Edition

Coding The MainForm’sFormClosing Event Procedure

(continued)

Figure 7-35: Pseudocode for the MainForm’s FormClosing event procedure

Page 40: Chapter 7: Sub and Function Procedures

40Programming with Microsoft Visual Basic 2005, Third Edition

Coding The MainForm’sFormClosing Event Procedure

(continued)

Figure 7-36: Message box displayed by the FormClosing event procedure

Page 41: Chapter 7: Sub and Function Procedures

41Programming with Microsoft Visual Basic 2005, Third Edition

Summary – Lesson C

• FormClosing event occurs when Me.Close () is called

• FormClosing event also occurs when Close button is clicked

• Form’s FormClosing event procedure is processed before a form is about to close