Assigning a return value to a function
One of the statements that you typically include in the function definition assigns the function areturn value, that is, a value that it returns to the caller.
The syntax is:
FunctionName = returnValue,
where returnValue has the data type specified in the As dataType clause of the function's signature: a scalar, a Variant, or an object reference.
For example:
Function Cubit(intArg%) As Double
' Return the cube of intArg%.
Cubit# = intArg% ^ 3
End Function
or
Function Left5(aString As String) As String
' Return the leftmost 5 characters of aString$.
Left5$ = Left$(aString$, 5)
End Function
You can cause a function to return an array or a list. To do so, you need to make the function's return value a Variant, which can hold an array or list, as in the following example, which passes an array of names in one format (first name, space, last name) to a function that returns another array in which the names appear in a different format (last name, comma, space, first name):
Dim myVariantVarV As Variant
Dim anArray(1 to 3) As String
Dim X As Integer
anArray$(1) = "Alex Smith"
anArray$(2) = "Elizabeth Jones"
anArray$(3) = "Martin Minsky"
Function SwitchNames(arrayOfNames() As String) As Variant
' Declare a local array variable to pass back to the
' application as the return value of SwitchNames.
' Performing the operation on arrayOfNames, which is
' passed by reference, would change anArray if
' arrayOfNames were the return value of the function.
Dim newArrayOfNames(1 to 3) As String
Dim tempArray(1 to 2, 1 to 3) as String
Dim aSpace As Integer
For X% = 1 to 3
' Locate the space that separates first name from
' last name in arrayOfNames, then extract everything
' before the space to tempArray, then extract
' everything after the space to the corresponding
' location in tempArray's second dimension.
aSpace% = Instr(arrayOfNames$(X%), " ")
tempArray$(1, X%) = Mid$(arrayOfNames$(X%), 1 , _
aSpace% - 1)
tempArray$(2, X%) = Mid$(arrayOfNames$(X%), aSpace% + 1, _
Len(arrayOfNames$(X%)))
Next
For X% = 1 to 3
newArrayOfNames(X%) = tempArray(2, X%) & ", " & _
tempArray(1, X%)
Next
SwitchNames = newArrayOfNames
End Function
MyVariantVarV = SwitchNames(anArray())
For X% = 1 to 3
print myVariantVarV(x%)
Next
' Output: Smith, Alex
' Jones, Elizabeth
' Minsky, Martin
For x% = 1 to 3
Print anArray(x%)
Next
' Output: Alex Smith
' Elizabeth Jones
' Martin Minsky
A function need not contain a statement that assigns it a return value. If you don't include a statement when you define the function, LotusScript® assigns the function the default return value appropriate to the data type specified or implied in the function signature. The default values are 0 for a numeric data type, the empty string ("") for a String, EMPTY for a Variant, and NOTHING for an object reference.
For example:
Dim anInt As Integer
Dim anotherInt As Integer
Function PrintCube(intArg%) As Integer
Print intArg% ^ 3
End Function
anInt% = CInt(InputBox$("Enter a number:"))
' Suppose the user enters 3.
anotherInt% = PrintCube%(anInt%)
' Output: 27
Print anotherInt%
' 0