1 / 53

Chapter 7: Sub and Function Procedures

Chapter 7: Sub and Function Procedures. Previewing the Cerruti Company Application. Figure 7-1 Interface showing the payroll calculations. Previewing the Cerruti Company Application (cont.). Figure 7-2 Message box containing a confirmation message. Lesson A Objectives.

mahdis
Download Presentation

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

  2. Previewing the Cerruti Company Application Figure 7-1 Interface showing the payroll calculations Programming with Microsoft Visual Basic 2012

  3. Previewing the Cerruti Company Application(cont.) Figure 7-2 Message box containing a confirmation message Programming with Microsoft Visual Basic 2012

  4. Lesson A Objectives After studying Lesson A, you should be able to: • Create and call an independent Sub procedure • 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 Programming with Microsoft Visual Basic 2012

  5. Sub Procedures • Procedure • A block of program code that performs a specific task • Two types of Sub procedures in Visual Basic: • Event procedure • A procedure associated with a specific object and event • Independent Sub procedure • Independent of any object and event • Processed only when called (invoked) by a Call statement Programming with Microsoft Visual Basic 2012

  6. Sub Procedures (cont.) Figure 7-3 Syntax and examples of an independent Sub procedure and the Call statement Programming with Microsoft Visual Basic 2012

  7. Passing Variables • Passing by value • Passes a copy of the variable’s value • Example: Disclosing your bank account balance • Passing by reference • Passes a variable’s address • Example: Disclosing your bank account number Programming with Microsoft Visual Basic 2012 Figure 7-4 Illustrations of passing by value and passing by reference

  8. Passing Variables (cont.) Passing Variables by Value • Provides only the contents of the variable to the receiving procedure • How to pass by value: • Include the keyword ByVal before the parameter • Reasons to pass by value: • The procedure needs to know the contents of the variable • The procedure does not need to change the original value • By default, Visual Basic passes by value Programming with Microsoft Visual Basic 2012

  9. Passing Variables (cont.) Passing Variables by Value (cont.) Figure 7-5 Favorite Title application’s interface Programming with Microsoft Visual Basic 2012 Figure 7-6 Partially-coded btnDisplay_Click event procedure

  10. Passing Variables (cont.) Passing Variables by Value (cont.) Figure 7-7 DisplayMsg procedure Programming with Microsoft Visual Basic 2012 Figure 7-8 Message shown in the interface

  11. Passing Variables (cont.) Passing Variables by Value (cont.) Figure 7-9 DisplayMsg procedure header and Call statement Programming with Microsoft Visual Basic 2012

  12. Passing Variables (cont.) Passing Variables by Reference • Provides the address (memory location) of the variable to the procedure • The receiving procedure can thus access the variable • Reason to pass by reference: • The procedure needs to change the variable’s contents • How to pass by reference: • Include the keyword ByRef before the parameter Programming with Microsoft Visual Basic 2012

  13. Passing Variables (cont.) Passing Variables by Reference (cont.) Figure 7-11 btnCalc_Click procedure from Chapter 6 Figure 7-10 Gross Pay application’s interface Programming with Microsoft Visual Basic 2012

  14. Passing Variables (cont.) Passing Variables by Reference (cont.) Figure 7-12 Call statement entered in the procedure Figure 7-13 CalcGross procedure Figure 7-14 Gross pay shown in the interface Programming with Microsoft Visual Basic 2012

  15. Passing Variables (cont.) Passing Variables by Reference (cont.) Figure 7-15 CalcGross and btnCalc_Click procedures Programming with Microsoft Visual Basic 2012

  16. Passing Variables (cont.) Passing Variables by Reference (cont.) Figure 7-16 Desk-check table before the Call statement is processed Programming with Microsoft Visual Basic 2012 Figure 7-17 Desk-check table after the Call statement and CalcGross procedure header are processed

  17. Passing Variables (cont.) Passing Variables by Reference (cont.) Figure 7-18 Desk-check table after the statement in the CalcGross procedure is processed Programming with Microsoft Visual Basic 2012

  18. Function Procedures • Function procedure • A block of code that performs a specific task • Returns a value after completing its task • Visual Basic provides built-in functions • You can also create your own functions • AsdataType in header indicates the return type of data • The Return statement is typically the last statement in a function • The Returnexpression type must agree with the AsdataType Programming with Microsoft Visual Basic 2012

  19. Function Procedures (cont.) Figure 7-22 Syntax and examples of functions Programming with Microsoft Visual Basic 2012

  20. Function Procedures (cont.) Figure 7-23 Examples of invoking the GetNewPrice function Programming with Microsoft Visual Basic 2012

  21. Function Procedures (cont.) Figure 7-24 CalcGross function and btnCalc_Click procedure Programming with Microsoft Visual Basic 2012

  22. Lesson A Summary • There are two types of Sub procedures: • Event • Independent • A function performs a task and returns a value • Independent procedures and functions are called from an application’s code using the Call statement • Passing by value sends a copy of the variable’s contents to a procedure or function • Passing by reference sends a variable’s address to a procedure or function Programming with Microsoft Visual Basic 2012

  23. Lesson B Objectives After studying Lesson B, you should be able to: • Include a combo box in an interface • Add items to a combo box • Select a combo box item from code • Determine the number of items in the list portion of a combo box • Sort the items in the list portion of a combo box • Determine the item either selected or entered in a combo box • Code a combo box’s TextChanged event procedure Programming with Microsoft Visual Basic 2012

  24. Including a Combo Box in an Interface • Combo box • Allows the user to select from a number of choices • Allows the user to type an entry not on the list • Can save space on a form • A list box only allows the user to select from a number of choices • DropDownStyle property • Three values: • Simple • DropDown (default) • DropDownList Programming with Microsoft Visual Basic 2012

  25. Including a Combo Box in an Interface (cont.) Figure 7-27 Examples of the combo box styles Programming with Microsoft Visual Basic 2012

  26. Including a Combo Box in an Interface (cont.) Figure 7-28 Code associated with the combo boxes in Figure 7-27 Programming with Microsoft Visual Basic 2012

  27. Including a Combo Box in an Interface (cont.) Figure 7-29 Correct TabIndex values Figure 7-30 Gross pay amount shown in the interface Programming with Microsoft Visual Basic 2012

  28. Including a Combo Box in an Interface (cont.) Figure 7-31 Modified code for the Gross Pay application Programming with Microsoft Visual Basic 2012

  29. Lesson B Summary • To add a combo box to a form: • Use the ComboBox tool in the toolbox • To specify the style of a combo box: • Set the combo box’s DropDownStyle property • To add items to a combo box: • Use the Items collection’s Add method • The method’s syntax is object.Items.Add(item) • In the syntax, object is the name of the combo box, and item is the text you want added to the list portion of the control Programming with Microsoft Visual Basic 2012

  30. Lesson B Summary (cont.) • To automatically sort the items in the list portion of a combo box: • Set the combo box’s Sorted property to True • To determine the number of items in the list portion of a combo box: • Use the Items collection’s Count property • Its syntax is object.Items.Count, in which object is the name of the combo box • To select a combo box item from code: • Use any of the following properties: SelectedIndex, SelectedItem, or Text Programming with Microsoft Visual Basic 2012

  31. Lesson B Summary (cont.) • To determine the item either selected in the list portion of a combo box or entered in the text portion: • Use the combo box’s Text property • However, if the combo box is a DropDownList style, you also can use the SelectedIndex or SelectedItem property • To process code when the value in a combo box’s Text property changes: • Enter the code in the combo box’s TextChanged event procedure Programming with Microsoft Visual Basic 2012

  32. Lesson C Objectives After studying Lesson C, you should be able to: • Prevent a form from closing • Round a number Programming with Microsoft Visual Basic 2012

  33. Creating the Cerruti Company Application Figure 7-33 TOE chart for the Cerruti Company application Programming with Microsoft Visual Basic 2012

  34. Creating the Cerruti Company Application (cont.) Figure 7-34 User interface for the Cerruti Company application Programming with Microsoft Visual Basic 2012

  35. Coding the FormClosing Event Procedure • FormClosing event • Occurs when a form is about to be closed because: • The computer processes the Me.Close() statement • The user clicks the Close button on the form’s title bar • Requirements for the FormClosing event procedure: • Verifying that the user wants to close the application • Taking appropriate action based on the user’s response • To prevent closing, set the Cancelproperty of the FormClosing procedure’s e parameter to True Programming with Microsoft Visual Basic 2012

  36. Coding the FormClosing Event Procedure (cont.) Figure 7-35 Pseudocode for the FormClosing event procedure Figure 7-36 Message box displayed by the code in the FormClosing event procedure Programming with Microsoft Visual Basic 2012

  37. Coding the btnCalc_Click Procedure Figure 7-37 Pseudocode for the btnCalc_Click procedure Programming with Microsoft Visual Basic 2012

  38. Coding the btnCalc_Click Procedure (cont.) Figure 7-38 Listing of named constants and variables for the btnCalc_Click procedure Programming with Microsoft Visual Basic 2012

  39. Coding the btnCalc_Click Procedure (cont.) Figure 7-39 Selection structure entered in the procedure Figure 7-40 Second selection structure entered in the procedure Programming with Microsoft Visual Basic 2012

  40. Coding the btnCalc_Click Procedure (cont.) Creating the GetFwt Function • To calculate weekly taxable wages: • Multiply the number of withholding allowances by $73.08 (2012 withholding allowance value) • Subtract this result from the weekly gross pay • To determine federal withholding tax (FWT): • Evaluate the weekly taxable wages and filing status • Use data to look up the FWT in special FWT tables • The GetFwt function emulates the FWT table lookup Programming with Microsoft Visual Basic 2012

  41. Coding the btnCalc_Click Procedure (cont.) Creating the GetFwt Function (cont.) Figure 7-41 Weekly FWT tables for the year 2012 Programming with Microsoft Visual Basic 2012

  42. Coding the btnCalc_Click Procedure (cont.) Creating the GetFwt Function (cont.) Figure 7-42 Example of a FWT calculation Figure 7-43 Another example of a FWT calculation Programming with Microsoft Visual Basic 2012

  43. Coding the btnCalc_Click Procedure (cont.) Creating the GetFwt Function (cont.) Figure 7-44 Pseudocode for the GetFwt function Figure 7-45 GetFwt function header and footer Programming with Microsoft Visual Basic 2012

  44. Completing the btnCalc_Click Procedure Rounding Numbers • You must call the GetFwt function from the btnCalc’s Click event procedure • Math.Round function • Rounds a value to a specific number of decimal places • Syntax: Math.Round (value[,digits]) • value is the numeric expression to work on • digits is the integer indicating the number of places to the right of the decimal point Programming with Microsoft Visual Basic 2012

  45. Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.) Figure 7-46 Syntax and examples of the Math.Round function Programming with Microsoft Visual Basic 2012

  46. Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.) Figure 7-48 Payroll calculations using the first set of test data Figure 7-47 Data for testing the Cerruti Company’s application (continues) Programming with Microsoft Visual Basic 2012

  47. Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.) (continued) Figure 7-49 Payroll calculations using the second set of test data Figure 7-47 Data for testing the Cerruti Company’s application Programming with Microsoft Visual Basic 2012

  48. Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.) Figure 7-50 Cerruti Company application’s code (continues) Programming with Microsoft Visual Basic 2012

  49. Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.) (continued) Figure 7-50 Cerruti Company application’s code (continues) Programming with Microsoft Visual Basic 2012

  50. Completing the btnCalc_Click Procedure (cont.) Rounding Numbers (cont.) (continued) Figure 7-50 Cerruti Company application’s code (continues) Programming with Microsoft Visual Basic 2012

More Related