1 / 17

Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition

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.

jclevenger
Download Presentation

Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition

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. Clearly Visual Basic: Programming with Visual Basic 2010 2nd Edition Chapter 16 I Hear You Are Breaking Up (Sub Procedures)

  2. 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

  3. 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

  4. 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

  5. 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

  6. Figure 16-2 Syntax, example, and steps for creating an independent Sub procedure Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. Figure 16-14 Planning information for the Total Due Calculator application Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  16. 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

  17. 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

More Related