1 / 15

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

Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition. Chapter 17 Talk To Me (Function Procedures). Objectives. After studying Chapter 17, you should be able to: Explain the difference between a Sub procedure and a Function procedure Create a Function procedure

corby
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 17 Talk To Me (Function Procedures)

  2. Objectives After studying Chapter 17, you should be able to: • Explain the difference between a Sub procedure and a Function procedure • Create a Function procedure • Invoke a Function procedure Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  3. What’s the Answer? • Function procedure • Returns a value after performing its assigned task • Can receive information either by value or by reference • Information it receives is listed in the parameterList in the header • Sub procedure • Does not return a value • Return statement • Usually the last statement in the function Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  4. Figure 17-1 Syntax, examples, and steps for creating a Function procedure Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  5. What’s the Answer? (cont’d.) • After creating a function: • You can invoke it from one or more places in an application’s code • To invoke a function: • Include the function’s name, along with any arguments, in a statement • Usually the statement that invokes a function will assign the function’s return value to a variable • May also use the return value in a calculation Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  6. Figure 17-2 Examples of invoking the GetNewPrice function Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  7. Price Calculator Application • Figure 17-3 • Shows the Price Calculator application’s user interface • Variables in a function header’s parameterList: • Have procedure scope Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  8. Price Calculator Application (cont’d.) Figure 17-3 Interface for the Price Calculator application Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  9. Revisiting the Total Due Calculator Application • Figure 17-10 • Shows interface for the Total Due Calculator application from Chapter 16 • Figure 17-11 • Shows the code entered in both the AssignDiscount Sub procedure and btnCalc control’s Click event procedure in Chapter 16 Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  10. Revisiting the Total Due Calculator Application (cont’d.) Figure 17-10 Interface for the Total Due Calculator application Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  11. Figure 17-11 AssignDiscount Sub procedure and btnCalc control’s Click event procedure (continues) Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  12. Figure 17-11 AssignDiscount Sub procedure and btnCalc control’s Click event procedure (cont’d.) Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  13. Which Way Is Better? • Two ways to assign the discount in the Total Due Calculator application • Independent Sub procedure • Passes a memory location by reference • Function • Avoid allowing more than one procedure to change contents of a memory location • Pass a variable by reference only when a procedure needs to produce more than one result Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  14. Summary • You can create your own Function procedures, referred to as functions • A function returns a value after completing its task • Return statement • Appears as the last statement in a function • Invoke a function by: • Including the function’s name, along with any arguments, in a statement Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

  15. Summary (cont’d.) • The statement that invokes a function may: • Assign the return value to a variable • Use the return value in a calculation • Display the return value • Variables that appear in a function header’s parameterListhave procedure scope • In most cases: • Better to use a function rather than a Sub procedure that passes a variable by reference Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition

More Related