Micro Focus QTP (UFT) Forums

Full Version: Unable to return value from a user-defined function to the calling Test
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[/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.
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
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?
That is the logic for VB Scripting. For C language, you can use return to get the returning result from a function.