Chapter 7: Sub and Function Procedures

Slides:



Advertisements
Similar presentations
1.
Advertisements

Sub and Function Procedures
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
The Web Warrior Guide to Web Design Technologies
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
Programming with Microsoft Visual Basic 2005, Third Edition
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA By Robert T. Grauer Maryann Barber.
String Variables Visual Basic for Applications 4.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Chapter Four The Selection Structure
Chapter 4: The Selection Structure
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Chapter 6: The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA.
Programming with Microsoft Visual Basic th Edition
1.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
An Introduction to Programming with C++1 Void Functions Tutorial 5.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Microsoft Visual Basic 2008: Reloaded Third Edition
Chapter 3: Using Variables and Constants
Repeating Program Instructions
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS16 Application Development and Programming using Visual Basic.net
CIS 16 Application Development Programming with Visual Basic
Presentation transcript:

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 Explain the difference between passing data by value and passing data by reference Create a Function procedure Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition

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

Procedures Procedure Two types of procedures in Visual Basic 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

Sub Procedures Event procedure Independent 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

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

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

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

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

Passing By Value (continued) Figure 7-4: Code for the Pet Information application Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition

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

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

Passing By Reference (continued) Figure 7-8: Code for the Gross Pay application Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition

Function Procedures (continued) Figure 7-11: Syntax and an example of a Function procedure Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition

The Pine Lodge Application (continued) Figure 7-12: Sample run of the Pine Lodge application Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition

Coding The Harvey Industries Payroll Application 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

Coding The Harvey Industries Payroll Application 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

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

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

Including a Combo Box in an Interface 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

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

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

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

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

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

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

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

Coding the GetFwt Function (continued) Figure 7-30: FWT calculations for Single taxpayers Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition

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

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

Completing the Harvey Industries Payroll Application Lesson C Objectives Prevent a form from closing Programming with Microsoft Visual Basic 2005, Third Edition

Coding The MainForm’s FormClosing Event Procedure 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

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

Coding The MainForm’s FormClosing Event Procedure (continued) Figure 7-36: Message box displayed by the FormClosing event procedure Programming 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 Programming with Microsoft Visual Basic 2005, Third Edition