Function (statement)

<< Click to Display Table of Contents >>

Function (statement)

Function Name [(ArgumentList)] [As Type]

 [Statements]

 Name = Expression

End Function

Begins the definition of a user-defined function.

Name is the name of the function.

ArgumentList is a comma-separated list of parameter declarations (see below).

Type specifies the data type (String, Integer, Double, Long, Variant). Alternatively, the type can be indicated by appending a type suffix (e.g. % for Integer) to the function name (see the section Data types).

The function definition ends with End Function. The Exit Function statement can be used to exit a function prematurely.

Declaring parameters

[ByVal | ByRef] Variable [As Type]

The keywords ByVal or ByRef (default value) are used to indicate whether the parameter is passed by value or by reference (see the section Passing parameters via ByRef or ByVal).

Type specifies the data type (String, Integer, Double, Long, Variant). Alternatively, the type can be indicated by appending a type suffix (e.g. % for Integer) to the variable name (see the section Data types).

See also: Dim, End, Exit, Sub

Example:

Sub Main

 For i% = 1 to 10

 Print GetColor2(i%)

 Next i

End Sub

Function GetColor2(c%) As Long

 GetColor2 = c% * 25

 If c% > 2 Then

         GetColor2 = 255           ' 0x0000FF - Red

 End If

 If c% > 5 Then

         GetColor2 = 65280         ' 0x00FF00 - Green

 End If

 If c% > 8 Then

         GetColor2 = 16711680      ' 0xFF0000 - Blue

 End If

End Function