150 likes | 508 Views
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
E N D
Clearly Visual Basic: Programming with Visual Basic 2010 2nd 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 • Invoke a Function procedure Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
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
Figure 17-1 Syntax, examples, and steps for creating a Function procedure Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
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
Figure 17-2 Examples of invoking the GetNewPrice function Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
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
Price Calculator Application (cont’d.) Figure 17-3 Interface for the Price Calculator application Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
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
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
Figure 17-11 AssignDiscount Sub procedure and btnCalc control’s Click event procedure (continues) Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition
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
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
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
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