Clearly Visual Basic Programming with Visual Basic 2008
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 18 Talk To Me
Objectives • 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 2008 2
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 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 Answer? (continued) • 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 2008 6
Clearly Visual Basic: Programming with Visual Basic 2008 7
Clearly Visual Basic: Programming with Visual Basic 2008 8
Price Calculator Application • Figure 18 -3 – Shows the Price Calculator application’s user interface • The variables in a function’s header – Have procedure scope Clearly Visual Basic: Programming with Visual Basic 2008 9
Clearly Visual Basic: Programming with Visual Basic 2008 10
Revisiting the Total Due Calculator Application • Figure 18 -8 – Shows interface for the Total Due Calculator application from Chapter 17 • Figure 18 -9 – Shows the code entered in both the Assign. Discount sub procedure and btn. Calc control’s Click event procedure in Chapter 17 • Most programmers: – Pass a variable by reference only when a procedure needs to produce more than one result Clearly Visual Basic: Programming with Visual Basic 2008 11
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 • You can create your own function procedures, called functions • 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 2008 15
Summary (continued) • 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 • The following have procedure scope – Variables that appear in a function header’s parameter. List • 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 2008 16
- Slides: 16