Function Procedures

Function procedures are very much like subprocedures in that they consist of Visual Basic statements and take arguments. Unlike subprocedures, function procedures begin with a Function statement and end with an End Function statement. Function procedures return values to the calling procedure by assigning a value to the function name:

Public Function MultiplyTwoNumbers(dNumber1 As Double, dNumber2 As Double)

MultiplyTwoNumbers = dNumberl * dNumber2 End Function

The MultiplyTwoNumbers function procedure takes two arguments and assigns the result of their multiplication to the function name, thereby returning the result to the calling function.

lblResult.Caption = MultiplyTwoNumbers(6, 9)

To be more dynamic, I could pass Value properties of two text boxes directly in as arguments.

lblResult.Caption = MultiplyTwoNumbers(Val(txtNumber1.Value), _ Val(txtNumber2.Value))

To ensure that the MultiplyTwoNumbers function receives numbers (doubles) as arguments, I use the Val function inside the parameter list to convert strings to numbers.

0 0

Post a comment