Clearly Visual Basic Programming with Visual Basic 2010
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 Invoke a Function procedure 2 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 parameter. List in the header Sub procedure Does not return a value Return statement Usually the last statement in the function 3 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 17 -1 Syntax, examples, and steps for creating a Function procedure 4 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 5 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 17 -2 Examples of invoking the Get. New. Price function 6 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Price Calculator Application Figure 17 -3 Shows the Price Calculator application’s user interface Variables in a function header’s parameter. List: Have procedure scope 7 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Price Calculator Application (cont’d. ) Figure 17 -3 Interface for the Price Calculator application 8 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 Assign. Discount Sub procedure and btn. Calc control’s Click event procedure in Chapter 16 9 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Revisiting the Total Due Calculator Application (cont’d. ) Figure 17 -10 Interface for the Total Due Calculator application 10 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 17 -11 Assign. Discount Sub procedure and btn. Calc control’s Click event procedure (continues) 11 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 17 -11 Assign. Discount Sub procedure and btn. Calc control’s Click event procedure (cont’d. ) 12 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 13 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 14 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd 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 parameter. List have procedure scope In most cases: Better to use a function rather than a Sub procedure that passes a variable by reference 15 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
- Slides: 15