Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
A Function procedure returns a value to the calling code either by executing a Return statement or by encountering an Exit Function or End Function statement.
To return a value using the Return statement
Put a
Returnstatement at the point where the procedure's task is completed.Follow the
Returnkeyword with an expression that yields the value you want to return to the calling code.You can have more than one
Returnstatement in the same procedure.The following
Functionprocedure calculates the longest side, or hypotenuse, of a right triangle, and returns it to the calling code.Function Hypotenuse(side1 As Double, side2 As Double) As Double Return Math.Sqrt((side1 ^ 2) + (side2 ^ 2)) End FunctionThe following example shows a typical call to
hypotenuse, which stores the returned value.Dim testLength, testHypotenuse As Double testHypotenuse = Hypotenuse(testLength, 10.7)
To return a value using Exit Function or End Function
In at least one place in the
Functionprocedure, assign a value to the procedure's name.When you execute an
Exit FunctionorEnd Functionstatement, Visual Basic returns the value most recently assigned to the procedure's name.You can have more than one
Exit Functionstatement in the same procedure, and you can mixReturnandExit Functionstatements in the same procedure.You can have only one
End Functionstatement in aFunctionprocedure.For more information and an example, see "Return Value" in Function Statement.