Micro Focus QTP (UFT) Forums
Optional parameter in function - 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: Optional parameter in function (/Thread-Optional-parameter-in-function)



Optional parameter in function - amit25007 - 08-19-2015

Hello

I am unable to put optional parameter in function.
below is an example

Code:
Public Function fun(var1, var2, var3, var4)
    Script
End Function

I don't want to enter value to var4 every time I use function. I want var4 to be optional.


Regards
Amit


RE: Optional parameter in function - abhideshpande001 - 08-19-2015

I think this is not possible. I would suggest Make two functions with 3 and 4 parameters and write it like this in script.
Code:
If Condition

Call Fn1(Var1, Var2, Var3)

Else

Call Fn2(Var1,Var2,Var3,Var4)



RE: Optional parameter in function - amit25007 - 08-19-2015

But with this method, i still don't have choice with Var4. The case is i will enter value of Var4 only in some specific cases, not always.


RE: Optional parameter in function - cuongtv - 08-20-2015

Another way, using Null or Empty argument:
Code:
Function myfunc(arg1,arg2,arg3,arg4)
If IsNull(arg1) OR IsEmpty(arg1)
      'ignore arg1
Else
      'use arg1
End If
If IsNull(arg2) OR IsEmpty(arg2)
      'ignore arg1
Else
      'use arg1
End If
.....
'Your code is here

End Function

To call your function:
Code:
Dim a
a = myfunc(NULL,arg2,,arg4)



RE: Optional parameter in function - abhideshpande001 - 08-20-2015

You can also use dynamic parameters using dictionary object/array.


RE: Optional parameter in function - vinod123 - 01-06-2016

Use switch case method to call the different variables in different cases


RE: Optional parameter in function - venkatesh9032 - 01-06-2016

If u dont want to pass the value in the argument u can leave empty instead of removing parameter.

Please find the snippet:


Code:
Fun Abc(Par1,Par2,Par3,Par4)

---
--
End Function

Call Abc("1","",2","3")