170 likes | 181 Views
Learn how to create, call, and pass data in Visual Basic 2010 Sub procedures for efficient programming. Understand the syntax, examples, and best practices. Master the art of breaking down complex tasks into manageable code blocks.
E N D
Clearly Visual Basic: Programming with Visual Basic 2010 2nd Edition Chapter 16 I Hear You Are Breaking Up (Sub Procedures)
Objectives After studying Chapter 16, you should be able to: • Create a Sub procedure • Call a Sub procedure • Pass data by value to a procedure • Pass data by reference to a procedure Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
What’s the Proper Procedure? • Sub procedures • Blocks of code that perform a specific task • Allow large and complex applications to be broken into small and manageable tasks • You can use a Sub procedure to: • Avoid duplicating code in different parts of a program • Keep an event procedure’s code from getting unwieldy and difficult to understand Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
What’s the Proper Procedure? (cont.) What’s the Proper Procedure? (cont’d.) Figure 16-1 Sample Click event procedure for an Exit button Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
What’s the Proper Procedure? (cont’d.) • Independent Sub procedure • Independent of any object and event • Has a procedure header and a procedure footer • Figure 16-2 • Shows syntax for creating an independent Sub procedure in Visual Basic Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Figure 16-2 Syntax, example, and steps for creating an independent Sub procedure Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
What’s the Proper Procedure? (cont’d.) • Convention for naming a Sub procedure • Enter procedure names using Pascal case • Common practice to begin a procedure’s name with a verb • Parameters • Store the information passed to the procedure when it is invoked Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
What’s the Proper Procedure? (cont’d.) • Call statement • Used to invoke a Sub procedure • procedureName • Name of the procedure you are calling (invoking) • argumentList • Comma-separated list of arguments • Argument • Represents information passed to the procedure when it is invoked Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
What’s the Proper Procedure? (cont’d.) Figure 16-3 Syntax and an example of the Call statement Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
The Weekly Pay Application • The application • Calculates and displays an employee’s regular pay, overtime pay, and gross pay • A text box’s TextChanged event • Occurs whenever a change is made to the contents of the text box Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Send Me Something • Number of arguments • Call statement should agree with the number of parameters in the procedure header • Passing by value • Passing a variable’s value • Passing by reference • Passing a variable’s address Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Just Give Me Its Value • To pass a variable by value in Visual Basic: • Include the keyword ByVal before the variable’s corresponding parameter in the receiving procedure’s parameterList • When you pass a variable by value: • Computer passes a copy of the variable’s contents to the procedure • You pass a variable by value when: • Receiving procedure needs to know variable’s contents but does not need to change the contents Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Where Do You Live? • You pass a variable by reference: • When you want the receiving procedure to change the contents of the variable • To pass a variable by reference: • Include the keyword ByRef before the variable’s corresponding parameter in the receiving procedure’s parameterList Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Where Do You Live? (cont’d.) Figure 16-13 Interface for the Total Due Calculator application Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Figure 16-14 Planning information for the Total Due Calculator application Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Summary • Sub procedures • Allow you to avoid duplicating code in different parts of a program • Common practice • To begin a procedure name with a verb and to enter the name using Pascal case • Call statement • Used to invoke an independent Sub procedure Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
Summary (cont’d.) • When calling a procedure: • The number of arguments listed in the Call statement’s argumentList should agree with the number of parameters listed in the receiving procedure’s parameterList • Passing information • By reference • By value Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition