Function Statement

   
Function Statement

Syntax

 [   ClassBehavior   ][   AccessModifier   ] Function   name   _           [(   arglist   )] [As   type   ][(  )]     [   statements   ]    [   name = expression   ]    [   statements   ] End Function 
ClassBehavior (optional; Keyword)

One of the following keywords:

Overloads

Indicates that more than one declaration of this function exists (with different argument signatures). For more detail, see Chapter 4.

Overrides

For derived classes, indicates that the function overrides the function by the same name (and argument signature) in the base class. For more detail, see Chapter 4.

Overridable

Indicates that the function can be overridden in a derived class. For more detail, see Chapter 4.

NotOverridable

Indicates that the function cannot be overridden in a derived class. For more detail, see Chapter 4.

MustOverride

Indicates that the function must be overridden in a derived class. For more detail, see Chapter 4.

Shadows

In a derived class definition, indicates that this element shadows any elements of the same name in the base class.

Shared

A shared function is callable without creating an object of the class. It is, in this strange sense, shared by all objects of the class. These are also called static functions.

AccessModifier (optional; Keyword)

One of the following keywords: Public , Private , Protected , Friend , Protected Friend . The following table describes the effects of the various access modifiers. Note that direct access refers to accessing the member without any qualification, as in:

 classvariable = 100 

and class/object access refers to accessing the member through qualification, either with the class name or the name of an object of that class:

 

Direct access scope

Class/object access scope

Private

Declaring class

Declaring class

Protected

All derived classes

Declaring class

Friend

Derived in-project classes

Declaring project

Protected Friend

All derived classes

Declaring project

Public

All derived classes

All projects

For more information, see Section 4.7 in Chapter 4.

name (required; String literal)

The name of the function.

arglist (optional)

A comma-delimited list of variables to be passed to the function as arguments from the calling procedure.

arglist uses the following syntax and parts :

 [Optional] [ByVal  ByRef] [ParamArray]   varname   [( )] [As   type   ] _     [=   defaultvalue   ] 
Optional (optional; Keyword)

An optional argument is one that need not be supplied when calling the function. However, all arguments following an optional one must also be optional. A ParamArray argument cannot be optional.

ByVal (optional; Keyword)

The argument is passed by value; that is, the local copy of the variable is assigned the value of the argument.

ByRef (optional; Keyword)

The argument is passed by reference; that is, the local variable is simply a reference to the argument being passed. All changes made to the local variable will be also reflected in the calling argument. ByVal is the default method of passing variables.

ParamArray (optional; Keyword)

Indicates that the argument is an optional array of Objects (or a strongly typed array, if Option Strict is on) containing an arbitrary number of elements. It can only be used as the last element of the argument list and cannot be used with the ByRef or Optional keywords.

varname (required; String literal)

The name of the local variable containing either the reference or value of the argument.

type (optional; Keyword)

The data type of the argument.

defaultvalue (optional; String literal)

For optional arguments, you must specify a default value.

type (optional; Keyword)

The return data type of the function.

statements (optional)

Program code to be executed within the function.

expression (optional)

The value to return from the function to the calling procedure.

Description

Defines a function procedure

Rules at a Glance

  • Overloads and Shadows cannot be used in the same declaration.

  • Functions cannot be nested; that is, you cannot define one function inside another function. (This applies to all procedures.)

  • If you do not include one of the access keywords, a function will be Public by default.

  • Any number of Exit Function statements can be placed within the function. Execution will continue with the line of code immediately following the call to the function. If a value has not been assigned to the function when the Exit Function statement executes, the function will return the default initialization value of the data type specified for the return value of the function. If the data type of the function was an object reference, the exited function will return Nothing .

  • The return value of a function is passed back to the calling procedure by either assigning a value to the function name or by using the Return statement. However, the Return statement also exits the function, whereas assigning the return value to the function name does not exit the function.

  • To return arrays of any type from a procedure, you must use parentheses after the data type in the return value of the function declaration, as in:

     Public Function Test() As Integer(  ) 
  • If you specify an optional parameter in your function declaration, you must also provide a default value for that parameter. For example:

     Private Function ShowMessage(Optional sMsg _                              As String = "Not given") 

Programming Tips and Gotchas

  • There is often confusion between using the ByRef and ByVal methods to assign arguments to a function. ByRef assigns a reference of the variable in the calling procedure to the variable in the function; any changes made to the variable from within the function are in reality made to the variable in the calling procedure. On the other hand, ByVal assigns the value of the variable in the calling procedure to the variable in the function. Changes made to the variable in the function have no effect on the variable in the calling procedure. In general, ByRef arguments within class modules take longer to perform, since marshaling back and forth between function and calling module must take place; so unless you explicitly need to modify a variable's value within a function, it's best to pass parameters by value.

  • Since a variable passed to a function by reference is actually modified by the function, you can use such variables to "return" multiple values from the function.

VB.NET/VB 6 Differences

  • If a parameter array is used in VB 6, it is a comma-delimited list of values in the calling procedure that is treated as an array of variants in the called function. In VB.NET, the arguments can be any data type, and they can be either a comma-delimited list of scalar values or an array.

  • In VB 6, the elements in parameter arrays are passed by reference; in VB.NET, they are passed by value.

  • If you do not specify whether an individual element in arglist is passed ByVal or ByRef , it is passed by reference in VB 6. In VB.NET, it is passed by value.

  • In VB 6, you can call a function that has arguments in a number of ways:

     x = SomeFunction(arg1, arg2) Call SomeFunction(arg1, arg2) SomeFunction arg1, arg2 

    In VB.NET, parentheses are required in the function call:

     x = SomeFunc(arg1, arg2) Call SomeFunc(arg1, arg2) SomeFunc(arg1, arg2) 
  • In VB 6, optional arguments do not require that you specify a default value. Instead, the IsMissing function is used to determine whether the optional argument is supplied (although in some cases it is unreliable). In VB.NET, you must assign a default value to an optional argument.

See Also

Sub Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net