User-Defined Functions
VW Scripting supports defining custom functions within a script. Once defined, a user function can be called just like a built-in function from any expression within the same scripting context.
Defining a Function
A function definition begins with Public or Private followed by Function or Sub, and ends with End Function or End Sub.
Public Function FunctionName(Param1, Param2)
' ... function body ...
Return result
End Function
| Keyword | Description |
|---|---|
Public |
The function is callable from expressions outside the script |
Private |
The function is for internal use only |
Function |
Returns a value via Return |
Sub |
Does not return a value |
Rules:
- The first non-blank line of the script must be the function declaration.
- The function name and parameters are case-insensitive when called.
- Parameters are matched positionally.
- All parameters must be provided when calling — no optional parameters.
Returning a Value
Use Return with a value to exit the function and return a result:
Public Function Add(a, b)
Return a + b
End Function
Use Return with no value (or reach End Function) to exit without a return value:
Public Sub LogMessage(msg)
Debug(msg)
End Sub
Examples
Simple calculation
Public Function Tax(amount, rate)
Return amount * rate / 100
End Function
Called as: Tax(Price, 13)
Conditional logic
Public Function Grade(score)
If score >= 90 Then
Return "A"
ElseIf score >= 80 Then
Return "B"
ElseIf score >= 70 Then
Return "C"
ElseIf score >= 60 Then
Return "D"
Else
Return "F"
End If
End Function
Called as: Grade(85) → "B"
Using built-in functions inside a user function
Public Function FullName(first, last)
Return Trim(first) & " " & Trim(last)
End Function
Loop inside a function
Public Function Factorial(n)
Dim result = 1
For i = 2 To n
result = result * i
Next
Return result
End Function
Called as: Factorial(5) → 120
Calling a User-Defined Function
Once defined, call the function exactly like any built-in function:
Dim t = Tax(99.99, 13)
Dim g = Grade(Score)
Note: User-defined functions are registered into the same function lookup table as built-in functions. If a user function has the same name as a built-in, the last one registered wins. Avoid naming user functions the same as built-ins.