Function Statement

A function is a block of code which runs when it is called. A function is usually called in an expression.

You can pass data, known as parameters or arguments, into a function. You may pass a parameter by value or by reference. When by reference, modifications applied to the parameter in the function will be sent back to the calling code.

A function usually returns data as a result.

Syntax:

Function Statement diagram


  [Private | Public] Function Name[char] (argument1 [As Type][, argument2[char][,...]]) [As typename]
          statements
      [Exit Function]
          statements
  End Function

Parametrar:

scope: Function default scope is Public. A Private scope denotes a module internal routine, not intended to be used from other modules.

name: Name of the subroutine to contain the value returned by the function.

arguments: Parameters to be passed to the subroutine.

argument fragment

argument fragment


      {[Optional [ByRef|ByVal]]|ParamArray} argument {{As typename|char}[ = expression]|[()]As Variant}
    
Parameters

Optional: The argument is not mandatory.

ByRef: The argument is passed by reference. ByRef is the default.

ByVal: The argument is passed by value. Its value can be modified by the called routine.

char: Type declaration character.

typename: Primitive data type name. Library or module defined types can also be specified.

= expression: Specify a default value for the argument, matching its declared type. Optional is necessary for each argument specifying a default value.

ParamArray: Use ParamArray when the number of parameters is undetermined. A typical scenario is that of a Calc user-defined function. Using ParamArray should be limited to the last argument of a routine.

tip

UsingParamArray or = expression require Option Compatible to be placed before the executable program code in a module.


warning

When using Option VBASupport 1, Optional arguments with no default value (= expression) are initialized according to their data type, except if Variant.


typename fragment

primitive data types fragment


      {Boolean|Byte|Currency|Date|Double|Integer|Long|Object|Single|String|Variant}
    
char fragment

type declaration characters


      { % | & | ! | # | $ | @ }
    

Examples:


Sub ExampleExit
Dim sReturn As String
Dim sListArray(10) As String
Dim siStep As Single
    For siStep = 0 to 10  ' Fyll matrisen med testdata
        sListArray(siStep) = chr$(siStep + 65)
        MsgBox sListArray(siStep)
    Next siStep
    sReturn = LinSearch(sListArray(), "B")
    Print sReturn
End Sub
 
Function LinSearch( sList(), sItem As String ) As Integer
Dim iCount As Integer
' Funktionen LinSearch söker igenom en TextArray:sList() efter en TextEntry:
' Returvärdet är elementets index eller 0 (Null)
    For iCount=1 To Ubound( sList() )
        If sList( iCount ) = sItem Then
            Exit For ' Hittade sItem
        End If
    Next iCount
    If iCount = Ubound( sList() ) Then iCount = 0
    LinSearch = iCount
End Function

Stötta oss!