Function Statement


Function Statement

Syntax

     [accessModifier] [procModifier] [Shared] [Shadows] _           Function   name [(Of typeParamList)] ([arglist]) _           [Implements implementsList | Handles eventList] [As type]        [statements]        [name = expression]        [Exit Function | Return expression]        [statements]     End Function 


accessModifier (optional)

Specifies the scope and accessibility of the function. One of the following access levels:

Access level

Description

Public

The function is publicly accessible anywhere, both inside and outside of the project.

Private

The function is accessible only within the defining type.

Protected

The function is accessible only to the code in the defining type or to one of its derived types.

Friend

The function is accessible only within the project that contains the function definition.

Protected Friend

Combines the access features of Protected and Friend.


If omitted, the Public access level is used.


procModifier (optional)

One of the keywords shown in the following table:

Keyword

Description

Overloads

Indicates that more than one declaration of this function exists, each with a different argument signature

Overrides

For derived classes, indicates that the function overrides a function with the same name and argument signature in the base class

Overridable

Indicates that the function can be overridden in a derived class

NotOverridable

Indicates that the function cannot be overridden in a derived class

MustOverride

Indicates that the function must be overridden in a derived class



Shared (optional)

Indicates that the function is shared and not an instance function. Shared functions may be called without a particular instance of the type in which they appear. Shared functions are also known as static functions.


Shadows (optional)

Indicates that the function shadows an identically named element in a base class.


name (required)

The name of the function.


typeParamName (optional; any)

Adds type parameter placeholders that will later enforce strong typing when the function is used. The Of clause implements generics, which are fully described in Chapter 10. If generics will not be used, this clause can be excluded.


arglist (optional; any)

A comma-delimited list of parameters to be supplied to the function as arguments from the calling routine.

     arglist uses the following syntax and parts:     [Optional] [ByVal | ByRef] [ParamArray] varname[(  )] _         [As argtype] [= defaultValue] 


Optional (optional)

Flags an argument as optional; optional arguments need not be supplied by the calling routine. All arguments following an optional argument must also be optional. A ParamArray argument cannot be optional.


ByVal (optional)

The argument is passed by value; the local copy of the variable is assigned the value of the argument. ByVal is the default method of passing variables.


ByRef (optional)

The argument is passed by reference; the local variable is a reference to the argument being passed. All changes made to the local variable will also be reflected in the calling argument.


ParamArray (optional)

The argument is an optional array containing an arbitrary number of elements. It can only be used as the last element of the argument list, and it cannot be modified by either the ByRef or Optional keywords. If Option Strict is on, the array type must also be specified.


varname (required)

The name of the argument as used in the local function.


argtype (optional; Type)

The data type of the argument. Any valid .NET data type can be used.


defaultValue (optional; any)

For optional arguments, indicates the default value to be supplied when the calling routine does not supply the value. When the Optional keyword is used, this default value is required.


implementsList (optional)

Comma-separated list of the interface members implemented by this function.


eventList (optional)

Comma-separated list of the events handled by this function. Each event is in the form eventVariable.eventMember, where eventVariable is a variable declared with the WithEvents keyword, and eventMember is an event member of that variable.


type (optional; Type)

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

The Function statement defines a function, including all arguments and the return value. Functions can appear within classes, structures, and modules. To call a function, specify its name, followed by any arguments in parentheses.

     result = SomeFunction(12, "second argument") 

The return value can be assigned to a variable, immediately used as a parameter for another function, or ignored by using the Call keyword.

     Call SomeFunction(12, "second argument") 

Usage at a Glance

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

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

  • Any number of Exit Function or Return statements can be placed within the function. When these statements are encountered, execution continues 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 is an object reference, the exited function returns 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. The Return statement also exits the function; assigning the return value to the function name does not exit the function.

  • To return arrays of any type from a procedure, follow type with parentheses:

         Public Function BuildIntArray(  ) As Integer(  ) 

  • The names of a function's parameters become the function's named arguments.

Version Differences

  • There are several syntax and functionality differences in the declaration of a function between VB 6 and the .NET version of VB.

  • In VB 6, arguments to functions are passed by reference if no passing method is specified. In .NET, the default is to pass by value.

  • If a parameter array is used in VB 6, it is an array of variants. In .NET, all parameter arrays are either of type Object or of some other specified type.

  • 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. In .NET, you must assign a default value to an optional argument.

  • Visual Basic 2005 adds support for generics to functions, as discussed in Chapter 10.

See Also

Sub Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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