Function Statement (Visual Basic)
Updated: August 2012
Declares the name, parameters, and code that define a Function procedure.
[ <attributelist> ] [ accessmodifier ] [ proceduremodifiers ] [ Shared ] [ Shadows ] Function name [ (Of typeparamlist) ] [ (parameterlist) ] [ As returntype ] [ Implements implementslist | Handles eventlist ] [ statements ] [ Exit Function ] [ statements ] End Function
Optional. See Attribute List.
Optional. Can be one of the following:
Optional. Can be one of the following:
Optional. See Shared.
Optional. See Shadows.
Required. Name of the procedure. See Declared Element Names (Visual Basic).
Optional. List of type parameters for a generic procedure. See Type List.
Optional. List of local variable names representing the parameters of this procedure. See Parameter List (Visual Basic).
Required if Option Strict is On. Data type of the value returned by this procedure.
Optional. Indicates that this procedure implements one or more Function procedures, each one defined in an interface implemented by this procedure's containing class or structure. See Implements Statement.
Required if Implements is supplied. List of Function procedures being implemented.
implementedprocedure [ , implementedprocedure ... ]
Each implementedprocedure has the following syntax and parts:
interfaceRequired. Name of an interface implemented by this procedure's containing class or structure.
definednameRequired. Name by which the procedure is defined in interface.
Optional. Indicates that this procedure can handle one or more specific events. See Handles Clause (Visual Basic).
Required if Handles is supplied. List of events this procedure handles.
eventspecifier [ , eventspecifier ... ]
Each eventspecifier has the following syntax and parts:
eventvariableRequired. Object variable declared with the data type of the class or structure that raises the event.
eventRequired. Name of the event this procedure handles.
Optional. Block of statements to be executed within this procedure.
Terminates the definition of this procedure.
All executable code must be inside a procedure. Each procedure, in turn, is declared within a class, a structure, or a module that is referred to as the containing class, structure, or module.
To return a value to the calling code, use a Function procedure; otherwise, use a Sub procedure.
Defining a Function
You can define a Function procedure only at the module level. Therefore, the declaration context for a function must be a class, a structure, a module, or an interface and can't be a source file, a namespace, a procedure, or a block. For more information, see Declaration Contexts and Default Access Levels (Visual Basic).
Function procedures default to public access. You can adjust their access levels with the access modifiers.
A Function procedure can declare the data type of the value that the procedure returns. You can specify any data type or the name of an enumeration, a structure, a class, or an interface. If you don't specify the returntype parameter, the procedure returns Object.
If this procedure uses the Implements keyword, the containing class or structure must also have an Implements statement that immediately follows its Class or Structure statement. The Implements statement must include each interface that's specified in implementslist. However, the name by which an interface defines the Function (in definedname) doesn't need to match the name of this procedure (in name).
Returning from a Function
When the Function procedure returns to the calling code, execution continues with the statement that follows the statement that called the procedure.
To return a value from a function, you can either assign the value to the function name or include it in a Return statement.
The Return statement simultaneously assigns the return value and exits the function, as the following example shows.
The following example assigns the return value to the function name myFunction and then uses the Exit Function statement to return.
The Exit Function and Return statements cause an immediate exit from a Function procedure. Any number of Exit Function and Return statements can appear anywhere in the procedure, and you can mix Exit Function and Return statements.
If you use Exit Function without assigning a value to name, the procedure returns the default value for the data type that's specified in returntype. If returntype isn't specified, the procedure returns Nothing, which is the default value for Object.
Calling a Function
You call a Function procedure by using the procedure name, followed by the argument list in parentheses, in an expression. You can omit the parentheses only if you aren't supplying any arguments. However, your code is more readable if you always include the parentheses.
You call a Function procedure the same way that you call any library function such as Sqrt, Cos, or ChrW.
You can also call a function by using the Call keyword. In that case, the return value is ignored. Use of the Call keyword isn't recommended in most cases. For more information, see Call Statement (Visual Basic).
Visual Basic sometimes rearranges arithmetic expressions to increase internal efficiency. For that reason, you shouldn't use a Function procedure in an arithmetic expression when the function changes the value of variables in the same expression.
The following example uses the Function statement to declare the name, parameters, and code that form the body of a Function procedure. The ParamArray modifier enables the function to accept a variable number of arguments.
The following example invokes the function declared in the preceding example.
Module Module1 Sub Main() ' In the following function call, calcSum's local variables ' are assigned the following values: args(0) = 4, args(1) = 3, ' and so on. The displayed sum is 10. Dim returnedValue As Double = calcSum(4, 3, 2, 1) Console.WriteLine("Sum: " & returnedValue) ' Parameter args accepts zero or more arguments. The sum ' displayed by the following statements is 0. returnedValue = calcSum() Console.WriteLine("Sum: " & returnedValue) End Sub Public Function calcSum(ByVal ParamArray args() As Double) As Double calcSum = 0 If args.Length <= 0 Then Exit Function For i As Integer = 0 To UBound(args, 1) calcSum += args(i) Next i End Function End Module