Friday, December 9, 2011

Function in VBScript


Here I have tried to throw light on the functions, arguments and return values which may be helpful for the beginners of QTP and VB Script.
1. What is function in VB Script
Ans: Function is a piece of code with specific keywords like
Function funtionName() 
End Function
Within these two key words we can write any number of lines of code to perform actions. Then we can call entire function with its function name wherever it is necessary. We can pass one or more than one variable/s as argument/s inside the function and can get one or more than one return value/s from it.
2. How to pass two arguments?
Ans: Let's say write a function to add two numbers
Dim num1,num2
num1=10
num2=20
Call addTwoNums(num1,num2)
Function addTwoNums(inNum1,inNum2)
   Dim outResult
   outResult=inNum1+inNum2
   msgbox outResult
End Function
This function works fine and gives result as 30 as perfect as the code is. But if you observe the function, result is still inside the function. If you just want to add two numbers and end up the looking at result, then this function is good enough. But what if you want to use the result of addTwoNums in another function to subtract?
To get the result outside we have following ways:
a. Define variable outResult as global variable. Means define outResult out side of function
Dim num1,num2
Dim outResult
num1=10
num2=20
Call addTwoNums(num1,num2)
Function addTwoNums(inNum1,inNum2)  
   outResult=inNum1+inNum2
End Function
msgbox outResult
b. In some cases we don't want to define outResult as global variable. Say there is another function subtractTwoNums to subtract and result is getting stored in outResult, if we execute first addTwoNums and next subtractTwoNums, outResult will be having result of subtractTwoNums. But because of some issue subtractTwoNums did not get execute even after calling it, then outResult will be having result of addTwoNums. So it misleads.
In such cases we can re-assign outResult to function it self. But while calling the function, we have to use a variable instead of Call keyword. Here is the code:
Dim num1,num2
num1=10
num2=20
rc= addTwoNums(num1,num2)
Function addTwoNums(inNum1,inNum2)  
   Dim outResult
   outResult=inNum1+inNum2
   addTwoNums=outResult
End Function
msgbox rc
c.Is there any other method without caring defining variable as public or private and not assigning result to function?
Ans: In such cases we have to add another argument and assign result to it. Here is the code:

Dim num1,num2
num1=10
num2=20
Call addTwoNums(num1,num2,result)
Function addTwoNums(inNum1,inNum2,outResult)
   outResult=inNum1+inNum2
End Function
msgbox result
NOTE: Don't define outResult as variable in side the function because outResult is argument.
d. Is there any way to make one function perform multiple actions like add,subtract,division and multiply?
Ans: Yes, here is the function:

Dim num1,num2,outResult
num1=10
num2=20
Call addTwoNums(num1,num2,result1,result2,result3,result4)
Function addTwoNums(inNum1,inNum2,outResult1,outResult2,outResult3,outResult4)
   'add
   outResult1=inNum1+inNum2
   'subtract
   outResult2=inNum1-inNum2
   'multiplication
   outResult3=inNum1*inNum2
   'devision
   outResult4=inNum1/inNum2
End Function
msgbox result1
msgbox result2
msgbox result3
msgbox result4
OMG so many arguments, if I want to pass 100 numbers and perform 200 actions how big my function's argument list LOL ;-) ;-) ;-)
Here is the solution to such scenarios. Use array function and make it every thing sweet and short.
Dim num(10),result(10)
num(1)=10
num(2)=20
Call addTwoNums(num,result)
Function addTwoNums(inNum,outResult)
   'add
   outResult(0)=inNum(1)+inNum(2)
   'subtrac
   outResult(1)=inNum(1)-inNum(2)
   'multiplication
   outResult(2)=inNum(1)*inNum(2)
   'devision
   outResult(3)=inNum(1)/inNum(2)
End Function

msgbox result(0)
msgbox result(1)
msgbox result(2)
msgbox result(3)
NOTE: Don't forget to define the variables as array before calling  the function (num(10),result(10)), you can define more space in array not less.

1 comment: