Clearly Visual Basic Programming with Visual Basic 2008
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 17 I Hear You Are Breaking Up
Objectives • • 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 2008 2
What’s the Proper Procedure? • 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 • Sub procedures – Allow large and complex applications to be broken into small and manageable tasks Clearly Visual Basic: Programming with Visual Basic 2008 3
Clearly Visual Basic: Programming with Visual Basic 2008 4
Clearly Visual Basic: Programming with Visual Basic 2008 5
What’s the Proper Procedure? (continued) • 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 • parameter. List – Specifies how each parameter is passed, either by value or by reference Clearly Visual Basic: Programming with Visual Basic 2008 6
What’s the Proper Procedure? (continued) • Call statement – Used to invoke a sub procedure • procedure. Name – Name of the procedure you are calling (invoking) • argument. List – Comma-separated list of arguments • Argument – Represents information that is passed to the procedure when it is invoked Clearly Visual Basic: Programming with Visual Basic 2008 7
Clearly Visual Basic: Programming with Visual Basic 2008 8
The Weekly Pay Application • The application – Calculates and displays an employee’s regular pay, overtime pay, and gross pay • A text box’s Text. Changed event – Occurs whenever a change is made to the contents of the text box Clearly Visual Basic: Programming with Visual Basic 2008 9
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 2008 10
Just Give Me Its Value • To pass a variable by value in Visual Basic: – Include the keyword By. Val before the variable’s corresponding parameter in the receiving procedure’s parameter. List • When you pass a variable by value: – Computer passes only the contents of the variable to the procedure • You pass a variable by value when: – Receiving procedure needs to know variable’s contents, but receiving procedure does not need to change the contents Clearly Visual Basic: Programming with Visual Basic 2008 11
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 By. Ref before the variable’s corresponding parameter in the receiving procedure’s header Clearly Visual Basic: Programming with Visual Basic 2008 12
Clearly Visual Basic: Programming with Visual Basic 2008 13
Clearly Visual Basic: Programming with Visual Basic 2008 14
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 Clearly Visual Basic: Programming with Visual Basic 2008 15
Summary (continued) • Call statement – Can be used to invoke a sub procedure • When calling a procedure: – The number of arguments listed in the Call statement’s argument. List should agree with the number of parameters listed in the receiving procedure’s parameter. List Clearly Visual Basic: Programming with Visual Basic 2008 16
- Slides: 16