NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

HxLink DID NOT INITIALIZE

Function Statement

Declares the name, arguments, and code that form the body of a Function procedure.

[Overloads]
[Overrides|CantOverride|MustOverride] [Protected]
[Public|Private|Protected|Friend|Protected Friend] Function name [(arglist)] [As type]
   [statements]

End Function

The Function statement syntax has these parts:

Part Description
Overloads Optional. Indicates that the Function procedure is overloaded. That is, more than one declaration exists, each with a distinct argument declaration that allows the compiler to distinguish which version to use.
Overrides Optional. Indicates that the Function procedure overrides an identically named function found in a base class. The data type of the arguments and the return value must exactly match those in the base class procedure.
CantOverride Optional. Indicates that the Function procedure cannot be overridden in a derived class.
MustOverride Optional. Indicates that a derived class must override the Function procedure in order to be creatable.
Protected Optional. Indicates that the Function procedure can be accessed by derived classes but not by other clients of the class.
Public Optional. Entities declared with the Public modifier have public access. There are no restrictions on the use of public entities.
Private Optional. Entities declared with the Private modifier have private access. A private entity is accessible only within its declaration context, including any nested entities.
Protected Optional. Entities declared with the Protected modifier have protected access. Protected access can only be specified on members of types (both regular type members and nested types), although there are different access rules for the two. A protected type member contained in a type is accessible to entities contained in a derived type, provided the access takes place through the derived type. A protected type nested in a type is accessible to entities contained in a derived type, provided the access takes place through the base type (since nested types are not inherited). Protected access is not a superset of friend access.
Friend Optional. Entities declared with the Friend modifier have friend access. An entity with friend access is accessible only within the program that contains the entity declaration.
Protected Friend Optional. Entities declared with the Protected Friend modifiers have the union of protected and friend accessibility.
name Required. Name of the Function; follows standard variable naming conventions.
arglist Optional. List of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas.
type Optional. Data type of the value returned by the Function procedure; may be Byte, Boolean, Char, Short, Integer, Long, Single, Double, Decimal, Date, String, or (except fixed length), Object, or any user-defined type. Arrays of any type cannot be returned, but an Object containing an array can be returned.
statements Optional. Any group of statements to be executed within the Function procedure.
expression Optional. Return value of the Function.

The arglist argument has the following syntax and parts:

[Optional] [ByVal | ByRef] [ParamArray] varname[( )] [As type] [= defaultvalue]

Part Description
Optional Optional. Indicates that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. Optional can't be used for any argument if ParamArray is used.
ByVal Optional. Indicates that the argument is passed by value. ByVal is the default in Visual Basic.
ByRef Optional. Indicates that the argument is passed by reference.
ParamArray Optional. Used only as the last argument in arglist to indicate that the final argument is an Optional array of Object elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. It may not be used with ByVal, ByRef, or Optional.
varname Required. Name of the variable representing the argument; follows standard variable naming conventions.
type Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, Char, Short, Integer, Long, Single, Double, Decimal Date, String (variable length only), Object, or a specific object type. If the parameter is not Optional, a user-defined type may also be specified.
defaultvalue Optional. Any constant or constant expression. Valid for Optional parameters only. If the type is an Object, an explicit default value can only be Nothing.

Remarks

Function procedures are public by default.

All executable code must be in procedures. You can't define a Function procedure inside another Function, Sub, or Property procedure.

The Exit Function statement causes an immediate exit from a Function procedure. Program execution continues with the statement following the statement that called the Function procedure. Any number of Exit Function statements can appear anywhere in a Function procedure.

Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements. However, unlike a Sub procedure, you can use a Function procedure on the right side of an expression in the same way you use any intrinsic function, such as Sqr, Cos, or Chr, when you want to use the value returned by the function.

You call a Function procedure using the function name, followed by the argument list in parentheses, in an expression. A function can also be called using the Call statement in which case the return value of the function will be ignored.

To return a value from a function, either assign the value to the function name or use the Return statement. Any number of such assignments can appear anywhere within the procedure. If no value is assigned to name, the procedure returns a default value: a numeric function returns 0 and a string function returns a zero-length string ("")Empty. A function that returns an object reference returns Nothing if no object reference is assigned to name (using Set) within the Function.

The following examples show how to assign a return value to a function named BinarySearch. In this case, False is assigned to the name to indicate that some value was not found.

Function BinarySearch(. . .) As Boolean
. . .
   ' Value not found. Return a value of False.
   If lower > upper Then
      BinarySearch = False
      Exit Function
   End If
. . .
End Function

In this second example, the value is not found and the Return statement gives us a False value.

Function BinarySearch(. . .) As Boolean
. . .
   ' Value not found. Return a value of False.
   If lower > upper Then
      Return False
      Exit Function
   End If
. . .
End Function

Variables used in Function procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure (using Dim or the equivalent) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure.

Caution   Visual Basic may rearrange arithmetic expressions to increase internal efficiency. Avoid using a Function procedure in an arithmetic expression when the function changes the value of variables in the same expression.

See Also

Example

AddressOf Operator | Call Statement | Dim Statement | Friend Keyword | Option Strict