1 / 41

Chapter 7: Sub and Function Procedures

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

nalani
Télécharger la présentation

Chapter 7: Sub and Function Procedures

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 7: Sub and Function Procedures Programming with Microsoft Visual Basic 2005, Third Edition

  2. Creating Sub and Function ProceduresLesson 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 byvalue and passing data by reference • Create a Function procedure Programming with Microsoft Visual Basic 2005, Third Edition

  3. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  4. Previewing the Completed Application (continued) Figure 7-1: Payroll amounts shown in the user interface Programming with Microsoft Visual Basic 2005, Third Edition

  5. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  6. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  7. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  8. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  9. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  10. Passing By Value (continued) Figure 7-4: Code for the Pet Information application (continued) Programming with Microsoft Visual Basic 2005, Third Edition

  11. Passing By Value (continued) Figure 7-4: Code for the Pet Information application Programming with Microsoft Visual Basic 2005, Third Edition

  12. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  13. Passing By Reference (continued) Figure 7-8: Code for the Gross Pay application (continued) Programming with Microsoft Visual Basic 2005, Third Edition

  14. Passing By Reference (continued) Figure 7-8: Code for the Gross Pay application (continued) Programming with Microsoft Visual Basic 2005, Third Edition

  15. Passing By Reference (continued) Figure 7-8: Code for the Gross Pay application Programming with Microsoft Visual Basic 2005, Third Edition

  16. 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 Asdatatype Programming with Microsoft Visual Basic 2005, Third Edition

  17. Function Procedures (continued) Figure 7-11: Syntax and an example of a Function procedure Programming with Microsoft Visual Basic 2005, Third Edition

  18. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  19. The Pine Lodge Application (continued) Figure 7-12: Sample run of the Pine Lodge application Programming with Microsoft Visual Basic 2005, Third Edition

  20. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  21. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  22. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  23. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  24. Harvey Industries (continued) Figure 7-15: Partially completed user interface for the Harvey Industries application Programming with Microsoft Visual Basic 2005, Third Edition

  25. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  26. Including a Combo Box in an Interface (continued) Figure 7-16: Examples of the combo box styles Programming with Microsoft Visual Basic 2005, Third Edition

  27. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  28. Including a Combo Box in an Interface (continued) Figure 7-17: Code corresponding to the combo boxes shown in Figure 7-16 Programming with Microsoft Visual Basic 2005, Third Edition

  29. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  30. Coding the xCalcButton’s Click Event Procedure (continued) Figure 7-22: Pseudocode for the xCalcButton’s Click event procedure Programming with Microsoft Visual Basic 2005, Third Edition

  31. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  32. Coding the GetFwt Function (continued) Figure 7-25: Weekly FWT tables (Single person) Programming with Microsoft Visual Basic 2005, Third Edition

  33. Coding the GetFwt Function (continued) Figure 7-30: FWT calculations for Single taxpayers Programming with Microsoft Visual Basic 2005, Third Edition

  34. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  35. Completing the xCalcButton’s Click Event Procedure (continued) Figure 7-34: Payroll calculations displayed in the interface Programming with Microsoft Visual Basic 2005, Third Edition

  36. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  37. Completing the Harvey Industries Payroll ApplicationLesson C Objectives • Prevent a form from closing Programming with Microsoft Visual Basic 2005, Third Edition

  38. 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 Programming with Microsoft Visual Basic 2005, Third Edition

  39. Coding The MainForm’sFormClosing Event Procedure (continued) Figure 7-35: Pseudocode for the MainForm’s FormClosing event procedure Programming with Microsoft Visual Basic 2005, Third Edition

  40. Coding The MainForm’sFormClosing Event Procedure (continued) Figure 7-36: Message box displayed by the FormClosing event procedure Programming with Microsoft Visual Basic 2005, Third Edition

  41. 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 Programming with Microsoft Visual Basic 2005, Third Edition

More Related