Micro Focus QTP (UFT) Forums
Unable to return value from a user-defined function to the calling Test - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: VB Scripting/Descriptive Programming (https://www.learnqtp.com/forums/Forum-VB-Scripting-Descriptive-Programming)
+--- Thread: Unable to return value from a user-defined function to the calling Test (/Thread-Unable-to-return-value-from-a-user-defined-function-to-the-calling-Test)



Unable to return value from a user-defined function to the calling Test - richagoyalk - 06-03-2009

[/align]Using the Insert->Function Definition Generator i created a new function mysum with two variables var1 and var2 (both having passmode = By value)

I want this function to add the 2 variables and return the sum to the calling test.

code for the function:
Code:
Public Function mysum(var1, var2)
       ' TODO: add function body here
       sum=var1+var2
       msgbox(var1 & "+" & var2 & "=" & sum)
       return(sum)
End Function

TestCode:
Code:
sum=mysum(1,2)
Reporter.ReportEvent micPass, "ReportStepName Pass", "Sum=" &sum

Result:
I get a Type mismatch error. I tried all combinations of function definitions like: e.g. int Public Function mysum(var1, var2) but i still get the type mismatch error.


RE: Unable to return value from a user-defined function to the calling Test - stephenlb4u - 06-03-2009

Hi Richa,

Use the below code, return is not the valid syntax

Code:
Public Function mysum(var1, var2)
' TODO: add function body here
Dim sum
sum=var1+var2
msgbox(var1 & "+" & var2 & "=" & sum)
mysum = sum
End Function


sum=mysum(1,2)
msgbox  "Sum=" &sum



RE: Unable to return value from a user-defined function to the calling Test - richagoyalk - 06-03-2009

Thanks...

But this is strange... So if one wants to return values, it has to be stored in a variable name same as that of the function.

Whats the logic behind this? could u explain?

And what is the datatype of this mysum variable?


RE: Unable to return value from a user-defined function to the calling Test - guocnc - 06-12-2009

That is the logic for VB Scripting. For C language, you can use return to get the returning result from a function.