There are several ways to return multiple values from functions. In this topic, we’re going to look over the 5 most common techniques to pass 2 or more values from functions. The 5 techniques are:
- Returning variables in Global scope
- Returning a Collection
- Returning Arrays
- Using Concatenated Strings
- Passing through the use of ByRef
Returning variables in Global scope
This can be achieved by declaring the variables outside the scope of the function. Here, we don’t need to pass the values through the function; but we can simply manipulate them within the function’s scope. Please note that if the same string is declared within the function, it loses its global scope – as it becomes local to the function. These variables can come from a function library or from the test script as long as they are outside the scope of the calling method. Code snippet:
Dim intNumber_1: intNumber_1 = 40 Dim intNumber_2: intNumber_2 = 80 Public Sub PassValues intNumber_1 = intNumber_1/4 intNumber_2 = intNumber_2/4 End Sub PassValues MsgBox "intNumber_1 = " & intNumber_1 &_ vbLf & "intNumber_2 = " & intNumber_2
Demo:
Returning a Collection
Another way to pass multiple values from a function is through the using of creating and passing Collections. We can use a collection object to store multiple values as keys/items. Code snippet:
Public Function PassValues(ByVal Num_1, ByVal Num_2) Set oDict = CreateObject( "Scripting.Dictionary" ) With oDict .Add "Num_1", Num_1/4 .Add "Num_2", Num_2/2 End With Set PassValues = oDict End Function Set colNumbers = PassValues(40,80) MsgBox "intNumber_1 = " & colNumbers.Item("Num_1") &_ vbLf & "intNumber_2 = " & colNumbers.Item("Num_2")
Demo:
Returning Arrays
This is quite a common technique. Each element in the array stores a variable that is then passed through the function. Code snippet:
Public Function PassValues(ByVal Num_1, ByVal Num_2) Dim arrArray: ReDim arrArray(2) arrArray(0) = Num_1/4 arrArray(1) = Num_2/2 PassValues = arrArray End Function arrNew = PassValues(40,80) MsgBox "intNumber_1 = " & arrNew(0) &_ vbLf & "intNumber_2 = " & arrNew(1)
Demo:
Concatenated Strings
I have seen the usage of this technique almost as frequently as the use of arrays. Here, two or more concatenated numbers/strings can be passed through the function with the help of a delimiter. Code snippet:
Public Function PassValues(ByVal Num_1, ByVal Num_2) Num_1 = Num_1/4 Num_2 = Num_2/2 PassValues = Num_1 & "," & Num_2 End Function sNum = PassValues(40,80) MsgBox "intNumber_1 = " & Split(sNum, ",")(0) &_ vbLf & "intNumber_2 = " & Split(sNum, ",")(1)
Demo:
Note above that, concatenated string was passed, it must be broken by using one of the delimiting techniques (split, left, right etc.)
Using ByRef to Pass Multiple Values
Please refer to the article Passing Parameters ByRef and ByVal for a detailed explanation of this technique. It can be used to pass multiple values in the following manner:
Dim intNumber_1: intNumber_1 = 40 Dim intNumber_2: intNumber_2 = 80 Public Sub PassValues(ByRef Num_1, ByRef Num_2) Num_1 = Num_1/4 Num_2 = Num_2/2 End Sub PassValues intNumber_1, intNumber_2 MsgBox "intNumber_1 = " & intNumber_1 &_ vbLf & "intNumber_2 = " & intNumber_2
Demo:
I hope you found this article helpful. Thanks for visiting Relevant Codes :)
If you have any questions, please ask them in the comments section. If your query is confidential, please use the Contact Form to send me an e-mail instead.
{ 3 comments… read them below or add one }
Hi Anshoo Arora,
I am new to qtp and vbscript. please give me an easy way to understand the logic in the below code.
I googled for regexp and found this to find the intergers in an string.
sText = "qtp 123 qtp 154" Set oRegExp = New RegExp oRegExp.Pattern = "\d+" oRegExp.Global = True set colMatches = oRegExp.Execute (sText) iThisItem = colMatches.count for each sItem in colMatches sNewValue = sNewValue&" "&sItem ' next msgbox sNewValuePlease tell me the For each…loop. when “sNewValue = sItem& ” “&SNewValue” i get 154 123 and in the above code i am getting 123 154. How?
Thank you very much
Md shaphi
Hi Staphi,
The code you have there uses the pattern: “\d+” which is used to match any occurence of a number within a given string. Thus, when the RegExp code you have in the post executes, it uses the “sText” string and matches any occurence within the string that is a number. Thus, you get the following output:
where, 154 is the first occurence of the match and 123 is the second. Similarly, if you would to match “any” alphabet, you can simply negate the “\d+” pattern by doing so: [^\d+]“. Example:
sText = "qtp 123 qtp 154" Set oRegExp = New RegExp oRegExp.Pattern = "[^\d]" oRegExp.Global = True set colMatches = oRegExp.Execute (sText) iThisItem = colMatches.count MsgBox iThisItem for each sItem in colMatches sNewValue = sNewValue&" "&sItem ' next msgbox sNewValueWhen the above code executes, you will have the following output: “qtp qtp”
Hi Anshoo,
“Pssing Multiple Values from Functions” post is really helpful to me. This website with your effort is really worth reading. I would say its really a best website for QTP, I had ever browsed before.
Thank you so much for your contribution towards QTP prof.
Keep Blogging n Posting.
Cheers,