Function Statement (Visual Basic)

Declares the name, parameters, and code that define a Function procedure.

Note

Visual Basic 2008 introduces lambda expressions, which enable you to define function expressions in line. For more information, see Function Expression and Lambda Expressions.

[ <attributelist> ] [ accessmodifier ] [ proceduremodifiers ] [ Shared ] [ Shadows ] 
Function name [ (Of typeparamlist) ] [ (parameterlist) ] [ As returntype ] [ Implements implementslist | Handles eventlist ]
    [ statements ]
    [ Exit Function ]
    [ statements ]
End Function

Parts

  • attributelist
    Optional. See Attribute List.

  • accessmodifier
    Optional. Can be one of the following:

    See Access Levels in Visual Basic.

  • proceduremodifiers
    Optional. Can be one of the following:

  • Shared
    Optional. See Shared.

  • Shadows
    Optional. See Shadows.

  • name
    Required. Name of the procedure. See Declared Element Names.

  • typeparamlist
    Optional. List of type parameters for a generic procedure. See Type List.

  • parameterlist
    Optional. List of local variable names representing the parameters of this procedure. See Parameter List.

  • returntype
    Required if Option Strict is On. Data type of the value returned by this procedure.

  • Implements
    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.

  • implementslist
    Required if Implements is supplied. List of Function procedures being implemented.

    implementedprocedure [ , implementedprocedure ... ]

    Each implementedprocedure has the following syntax and parts:

    interface.definedname

    Part

    Description

    interface

    Required. Name of an interface implemented by this procedure's containing class or structure.

    definedname

    Required. Name by which the procedure is defined in interface.

  • Handles
    Optional. Indicates that this procedure can handle one or more specific events. See Handles.

  • eventlist
    Required if Handles is supplied. List of events this procedure handles.

    eventspecifier [ , eventspecifier ... ]

    Each eventspecifier has the following syntax and parts:

    eventvariable.event

    Part

    Description

    eventvariable

    Required. Object variable declared with the data type of the class or structure that raises the event.

    event

    Required. Name of the event this procedure handles.

  • statements
    Optional. Block of statements to be executed within this procedure.

  • EndFunction
    Terminates the definition of this procedure.

Remarks

All executable code must be inside a procedure. Each procedure in turn is declared within a class, structure, or module, which is called the containing class, structure, or module.

Use a Function procedure when you need to return a value to the calling code. Use a Sub procedure when you do not need to return a value.

You can define a Function procedure only at module level. This means the declaration context for a function must be a class, structure, module, or interface, and cannot be a source file, namespace, procedure, or block. For more information, see Declaration Contexts and Default Access Levels.

Function procedures default to public access. You can adjust their access levels with the access modifiers.

You can call a Function procedure on the right side of an expression when you want to use the value returned by the function. You use the Function procedure the same way you use any library function such as Sqrt, Cos, or ChrW.

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 are not supplying any arguments. However, your code is more readable if you always include the parentheses.

A function can also be called using the Call statement, in which case the return value is ignored.

Rules

  • Return Type. The Function statement can declare the data type of the value it returns. You can specify any data type or the name of an enumeration, structure, class, or interface.

    If you do not specify returntype, the procedure returns Object.

  • Implementation. If this procedure uses the Implements keyword, the containing class or structure must also have an Implements statement immediately following its Class or Structure statement. The Implements statement must include each interface specified in implementslist. However, the name by which an interface defines the Function (in definedname) does not have to be the same as the name of this procedure (in name).

Behavior

  • Returning from a Procedure. When the Function procedure returns to the calling code, execution continues with the statement following the statement that called it.

    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.

  • Return Value. 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 following example assigns the return value to the function name myFunction and then uses the Exit Function statement to return.

    Function myFunction(ByVal j As Integer) As Double
        myFunction = 3.87 * j
        Exit Function 
    End Function
    

    If you use Exit Function without assigning a value to name, the procedure returns the default value for the data type specified in returntype. If returntype is not specified, the procedure returns Nothing, the default value for Object.

    The Return statement simultaneously assigns the return value and exits the function. The following example shows this.

    Function myFunction(ByVal j As Integer) As Double 
        Return 3.87 * j
    End Function
    

Troubleshooting

  • Order of Execution. Visual Basic sometimes rearranges arithmetic expressions to increase internal efficiency. For that reason, avoid using a Function procedure in an arithmetic expression when the function changes the value of variables in the same expression.

Example

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.

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

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

See Also

Tasks

How to: Use a Generic Class

Troubleshooting Procedures

Concepts

Parameter Arrays

Lambda Expressions

Reference

Sub Statement (Visual Basic)

Parameter List

Dim Statement (Visual Basic)

Call Statement (Visual Basic)

Of

Function Expression