Understanding Function Procedures and Sub Procedures in Visual Basic 2008
In this chapter of "Clearly Visual Basic: Programming with Visual Basic 2008," we explore the distinction between function procedures and sub procedures. Learn how to create and invoke a function procedure that returns a value and can receive information by value or by reference. We will also discuss the appropriate usage of variables within procedure scope, showcasing examples through applications like the Price Calculator and Total Due Calculator. This knowledge will enhance your programming skills and optimize your coding practices in Visual Basic.
Understanding Function Procedures and Sub Procedures in Visual Basic 2008
E N D
Presentation Transcript
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
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 Clearly Visual Basic: Programming with Visual Basic 2008
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
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
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 AssignDiscount sub procedure and btnCalc 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
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
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 parameterList • 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