Functions

A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing same code over and over again.

Apart from inbuilt Functions, VBScript allows us to write user-defined functions as well. This section will explain you how to call functions from inline or field/record functions.

IMan has a over 100 VBScript Function Reference and additional functions can be defined through Common Functions.

Calling a Function

To invoke a function somewhere you need to write the name of that function.

For example, to call the Now function.

Dim Result

Result = Now ' Returns the Current Date/Time

Result

Function Parameters

Functions can have parameters. Each parameter is passed to the function by enclosing the parameters in parentheses where each parameter is separated by a comma.

In the example below the Left function requires two parameters: the string, and the number of characters.

Dim Value
Dim Result

Value = "My Left String"

Result = Left(Value, 7)' Returns the first 7 characters of the string

Result

When the above code is executed the value of the Result variable is 'My Left'.

Optional Parameters

An optional parameter is a procedure or function argument marked with Optional where the parameter can be omitted. omitted, it uses its default value (or Empty if no default is provided).

Example 1 - Omitting the Optional Parameter

In the example below the Mid function requires has two mandatory parameters: the string, and the nth character to start; and an optional parameter the number of characters.

Dim Value
Dim Result

Value = "My Parsed String"

Result = Mid(Value, 11)' Returns all the characters from the 11th character

Result

When the above code is executed the value of the Result variable is 'String'.

Example 2 - Specifying the Optional Parameter

The following example specifies the 3rd parameter - the number of characters.

Dim Value
Dim Result

Value = "My Parsed String"

Result = Mid(Value, 11, 3)' Returns three characters from the 11th character

Result

When the above code is executed the value of the Result variable is 'Str'.

Defining your own functions

You can define your own Functions in Common Functions allowing you to define reusable logic. Functions can accept zero, one or more input values (parameters) and returns a result.

  • Function Header - The first line.
    • Starts with 'Function'
    • The function name.
    • Zero, one or more parameters Parameters are defined, by enclosing their names in parentheses.
  • Assigning the Result
    • The result of the function is explicitly assigned. E.g. FunctionName = <someVariable>
  • End Function - The last line.

Example 1 - A function with Parameters

The following example defines (a ridiculous example) a function named MyFunction with two parameters - num and character.

Function MyFunction(num, character)

  Dim Value
  Dim Result

  Value = String(number, character)

  MyFunction = Left(Value, 7) ' Assigns the result of the Left function to the result of the function.

End Function

The result of the function is defined on second last line.

Example 2 - Assigning The function Result in Different places

Contrary to Expressions where the last line becomes the result of the field/expression you can assign the result of a function in multiple places.

The below example shows result being assigned based on the value of the number parameter.

Function MyFunction(num, character)

  If number > 10 Then
    MyFunction = String(number, character)
  Else
    MyFunction = "The number is too small"
  End If

End Function