QTP: How can I return multiple Values from a Function
Asked Answered
N

11

9

I'm trying to write a function which can return multiple values from a Function which is having 2 arguments.

eg:

function sample_function(arg1,arg2)
    ''#Some code.................

    passenger = list1(0)
    name1 = list1(1)
    age1 = list1(2)
    seatNumber = list1(3)
    ''#This is an Incomplete function...    
end function sample_function

Here this function named sample_function has 2 argument named arg1, arg2. When i call this function in my Driver Script like value = sample_function(2,Name_person), this function should return me passenger, name1,age1,seatNumber values.

How can i achieve this?

EDIT (LB): QTP uses VBScript to specify the test routines, so I retagged this to VBScript, VB because the solution is probably within VBScript.

Nosography answered 4/8, 2010 at 7:52 Comment(0)
L
26

A straightforward solution would be to return an array:

function foo()
 foo=array("Hello","World")
end function

x=foo()

MsgBox x(0)
MsgBox x(1)

If you happen to use the same batch of values more often than once, then it might pay to make it a user defined class:

class User
 public name
 public seat
end class 

function bar()
 dim r 
 set r = new User
 r.name="J.R.User"
 r.seat="10"
 set bar=r
end function

set x=bar()

MsgBox x.name
MsgBox x.seat
Lutanist answered 4/8, 2010 at 8:13 Comment(0)
A
3

In VBScript, all function parameters are input/output parameters (also called ByRef parameters). So you could return your data simply using the function parameters. Example:

Function Test(a, b, c)
   a = 1
   b = 2
   c = 3
End Function

Test x, y, z
MsgBox x  ' Shows 1
MsgBox y  ' Shows 2
MsgBox z  ' Shows 3
Amersham answered 4/8, 2010 at 10:32 Comment(1)
@Fmunkert: i agree that this will also work, But this will not work when we have 2 arguments and there are 3 return types. @Luther: Thank u , this worked for me.....Nosography
T
1

You can create a new Datatype and add all necessary members in it. Then return this new datatype from your function.

Tankage answered 4/8, 2010 at 8:8 Comment(0)
B
1

You can do it by returning dictionary object from the function .

set dictFunRetun=foo()
msgbox dictFunRetun.item("Msg1")
msgbox dictFunRetun.item("Msg2")
function foo()
    set fnReturn=CreateObject("Scripting.Dictionary")
    fnReturn.Add "Msg1","First Variable"
    fnReturn.Add "Msg2","Second Variable"
    set foo=fnReturn
end function

here in dictionary i have added to keys named Msg1 and Msg2 similarly we can add more keys with having value of different types like int, Boolean ,array any type of data ..

Bereniceberenson answered 7/9, 2013 at 23:45 Comment(0)
O
1

declare your function like this.

function sample (arg1, arg2, passenger, name, age1, seatNumber)
''#Some code.................
passenger = list1(0)
name1 = list1(1)
age1 = list1(2)
seatNumber = list1(3)
''#This is an Incomplete function...
end function sample

then when you call it just input the variables you want to return.

Overshine answered 18/4, 2015 at 20:28 Comment(0)
H
0

This will Help you,

you can do it in two methods first one is to pass all values as array for example

function message

message1="Hello How are you?"

message2="i am fine"

message3="Hope you doing fine"

message=Array(message1,message2,message3)

End function

function chat-history

user-history=message

usermessage1=user-history(0)

usermessage2=user-history(1)

usermessage3=user-history(2)

End Function

The Second method is to concatenate and split

function message

message1="Hello How are you?"

message2="i am fine"

message3="Hope you doing fine"

message=message1+"@"+message2+"@"+message3

End function

function chat-history

user-history=message

user-message=split(user-history,"@")

usermessage1=user-message(0)

usermessage2=user-message(1)

usermessage3=user-message(2)

End Function
Hom answered 21/7, 2013 at 12:7 Comment(0)
B
0

I own nothing. All credit goes to the hardwork of my online friends pradeek and another friend. I hope this helps someone.

'Passing multiple numerical values from one function to another method-1

Function fnFirstFunction()
    value1=1
    value2=2
    value3=3
    A=Array(value1,value2,value3)
    fnFirstFunction=A
    'msgboxA(0)
    'msgboxA(1)
End Function

'Belowfunction passes the value returned from the fnFirstFunction and displays
Function fnSecondFunction(ByVal  A)
    P=A
    B=P(0)
    MsgBox B
    C=P(1)
    Msgbox C
    D=P(2)
    Msgbox D
End Function

A=fnFirstFunction()
'AsfnFirstFunction returns a value, take it to a variable
Call fnSecondFunction(A)'a

'passing multiple character values from one function to another method -2
public function fun1()

    msg1="Hello How are you?"
    msg2="i am fine"
    msg3="Hope you doing fine"

    arr1=Array(msg1,msg2,msg3)
    fun1=arr1
End function

arr1=fun1()
Call fun2(arr1)

public function fun2(byval arr1)
    j=arr1
    usermessage1=j(0)
    msgbox usermessage1
    usermessage2=j(1)
    msgbox usermessage2
    usermessage3=j(2)
    msgbox usermessage3
End Function

'Passing multiple values from one function to another method-3
public function fun1()
    msg1="Hello How are you?"
    msg2="i am fine"
    msg3="Hope you doing fine"
    arr1=msg1 + "@" + msg2 + "@" + msg3
    fun1=arr1
End function

arr1=fun1()
Call fun2(arr1)

public function fun2(byval arr1)
    j=arr1
    k= split(j,"@")
    usermessage1=k(0)
    msgbox usermessage1
    usermessage2=k(1)
    msgbox usermessage2
    usermessage3=k(2)
    msgbox usermessage3
End Function
Borax answered 7/10, 2014 at 7:38 Comment(0)
M
0

By using dictionary Object.This is your function

Function sample_function(ByRef passengerDetails)
    ''#Some code.................
    passengerDetails("passenger")=list(0)
    passengerDetails("name")=list(1)
    passengerDetails("age")=list(2)
    passengerDetails("seatnumber")=list(3)
    '#This is an Incomplete function...    
end function
'Creating a dictionary object
Set passengerDetails= CreateObject("Scripting.Dictionary")
'calling your function
sample_function(passengerDetails)

Advantage of using dictionary object is, sometime down the line you want the function to return more values(and this function is used by many projects/teams), you have to do is add the values you want to return in the dictionary object, without having to add more parameters(others using this function wont get any error)

Mistrot answered 28/11, 2016 at 16:38 Comment(0)
L
0

If you are sure that the list1 array carry only required data in same manner as you posted. Then you can directly pass the array from function too:-

function sample_function(arg1,arg2)
    ''#Some code.................

    passenger = list1(0)
    name1 = list1(1)
    age1 = list1(2)
    seatNumber = list1(3)
    sample_function list1    
end function 
Lindo answered 6/10, 2017 at 11:42 Comment(0)
E
-2
Option Explicit

Dim val1, val2
Dim res1, res2, res3, res4

Print "Calling a Sub function"

funcMath1 10, 25, "m"
funcMath1 10, 25, "S"
funcMath1 10, 25, "D"
funcMath1 10, 25, "A"
funcMath1 10, 25, "C"
Explain answered 17/9, 2013 at 13:13 Comment(1)
Hi, your post has been flagged as "low quality", probably because it consists solely of code. You could massively improve your answer by providing an explanation of exactly how and why this answers the question?Breaking
L
-3
'==============================================
' Date      :   09/21/2012  
' Created By    :   Jamil
' VB Script Basic Sub Function and Function Test
'==============================================

Option Explicit

Dim val1, val2
Dim res1, res2, res3, res4

Print "Calling a Sub function"

funcMath1 10, 25, "m"
funcMath1 10, 25, "S"
funcMath1 10, 25, "D"
funcMath1 10, 25, "A"
funcMath1 10, 25, "C"


'(1) simple calculater sub function with no return value


Function funcMath1(val1, val2, opt)
Dim result

Select Case UCase(opt)
    Case "M"
        result = (val1 * val2)
        Print "The result of multiplying is " &val1& " With " &val2& " is " &result
    Case "S" 
        result = (val1 - val2)
        Print "The result of subtraction is " &val1& " With " &val2& " is " & result
    Case "D" 
        result = (val1 / val2)
        Print "The result of divide is " &val1& " With " &val2& " is " & result
    Case "A" 
        result = (val1 + val2)
        Print "The result of Addtion is " &val1& " With " &val2& " is " & result
Case else
        msgBox "Your option "& opt &" is invalid!"
End Select

End Function



Print " "
Print "Calling a function"

call funcMath2(10, 25, "M")
call funcMath2(10, 25, "S")
call funcMath2(10, 25, "D")
call funcMath2(10, 25, "A")
call funcMath2(10, 25, "C")

'(2) simple calculater function with return value

Function funcMath2(val1, val2, opt)
Dim result, returnValue

Select Case UCase(opt)
    Case "M"
        result = (val1 * val2)
        Print "The result of multiplying is " &val1& " With " &val2& " is " &result
    Case "S" 
        result = (val1 - val2)
        Print "The result of subtraction is " &val1& " With " &val2& " is " & result
    Case "D" 
        result = (val1 / val2)
        Print "The result of divide is " &val1& " With " &val2& " is " & result
    Case "A" 
        result = (val1 + val2)
        Print "The result of Addtion is " &val1& " With " &val2& " is " & result
Case else
        msgBox "Your option "& opt &" is invalid!"
End Select

funcMath2 = result

End Function
Lecher answered 22/9, 2012 at 5:57 Comment(2)
As the oxymeron "sub function" indicates and the use of Call to ignore return values proves, this code snippet is a dangerous example of how not to program in VBScript. Furthermore it does not address the question of returning multiple values from a function.Timbered
What result? The functions return no result at all. So call cannot be wrong in this case ;-)Offspring

© 2022 - 2024 — McMap. All rights reserved.