How to: Call a Procedure That Returns a Value (Visual Basic)
A Function
procedure returns a value to the calling code. You call it by including its name and arguments either on the right side of an assignment statement or in an expression.
To call a Function procedure within an expression
Use the
Function
procedure name the same way you would use a variable. You can use aFunction
procedure call anywhere you can use a variable or constant in an expression.Follow the procedure name with parentheses to enclose the argument list. If there are no arguments, you can optionally omit the parentheses. However, using the parentheses makes your code easier to read.
Place the arguments in the argument list within the parentheses, separated by commas. Be sure you supply the arguments in the same order that the
Function
procedure defines the corresponding parameters.Alternatively, you can pass one or more arguments by name. For more information, see Passing Arguments by Position and by Name.
The value returned from the procedure participates in the expression just as the value of a variable or constant would.
To call a Function procedure in an assignment statement
Use the
Function
procedure name following the equal (=
) sign in the assignment statement.Follow the procedure name with parentheses to enclose the argument list. If there are no arguments, you can optionally omit the parentheses. However, using the parentheses makes your code easier to read.
Place the arguments in the argument list within the parentheses, separated by commas. Be sure you supply the arguments in the same order that the
Function
procedure defines the corresponding parameters, unless you are passing them by name.The value returned from the procedure is stored in the variable or property on the left side of the assignment statement.
Example
The following example calls the Visual Basic Environ to retrieve the value of an operating system environment variable. The first line calls Environ
within an expression, and the second line calls it in an assignment statement. Environ
takes the variable name as its sole argument. It returns the variable's value to the calling code.
MsgBox("Value of PATH is " & Environ("PATH"))
Dim currentPath As String = Environ("PATH")