You are on page 1of 4

What are procedures in VBScript and how they are used What are Subs in QTP Examples of how

ow you can create Subs in QTP Examples on passing parameters in Subs Functions in QTP and their features Creating Functions in QTP and passing parameters to a Function Difference between a Sub and a Function Example on how to return a value from a Function Passing parameters by Value and by Reference Lets get started with these concepts..

Procedures in QTP The grouping of the lines of code to execute it repeatedly can be broadly classified as Procedures. In QTP, you have two different types of procedures Sub Procedures and Functions. Both of them work the same way expect for some minor differences. Well cover both these types of procedures in the later part of the article.

Executing a Procedure in QTP Any Procedure (Function/Sub) in VBScript has two main aspects which are mandatory for executing it in VBScript or for that matter any other programming language. These are Function (Sub) Definition or Declaration: Function Definition is the actual code that you want to run as part of the function Function Call: This is a statement which executes your Function or Sub Lets see an example of this where we would create a function to find the sum of two numbers and display the result.

Sample Code 1: Structure of a Procedure (Function & Sub) 'Structure for Sub 1 '==================== 2 3 '1) Sub Definition - Actual Code 4 Sub fnSum() 5 var1 = 5 : var2 = 10 6 sum = var1 + var2 7 msgbox "The Sum of Numbers is: " & sum 8 End Sub 9 10 '2) Executing Sub 11 Call fnSum() 'Call keyword is not mandatory to execute a Sub 12 fnSum() 13 14 'Structure for Function 15 '====================== 16 17 '1) Function Definition - Actual Code 18 Function fnSum() 19 var1 = 5 : var2 = 10

sum = var1 + var2 20 msgbox "The Sum of Numbers is: " & sum 21 End Function 22 23 '2) Executing Function 24 Call fnSum() 'Call keyword is not mandatory to execute a Function 25 fnSum() 26 27 In the above example you can see that Call statement can also be used while calling a function/sub. To know more about Call statement you can check the difference between Call statement and normal function call.

Subs in QTP A Sub Procedure in QTP

- is a collection of statements that are enclosed between Sub and End Substatements. - can be used both with and without parameters. - does not return any value.

Passing Parameters in a Sub Procedure Consider a situation where you want to run the same set of code multiple times but with different set of data. In such a case, you can pass the data to your Sub in the form of parameters. This way you can make the code generic which can work with multiple set of data. Lets see an example for the same. Sample Code 2: Using Parameters in a Sub 1 Dim v1, v2 2 v1=5 : v2=10 3 4 fnSum v1, v2 'Passing parameters using variables fnSum 10, 20 'Passing parameters as literals 5 fnSum v1+10, v2*2 'Passing parameters as expression 6 7 'Sub Definition 8 Sub fnSum(var1, var2) 9 sum = var1 + var2 10 msgbox "The sum of numbers is: " & sum 11 End Sub 12

Functions in QTP A Function in QTP

- is a set of statements that are enclosed between Function and End Functionstatements. - can be used with and without parameters

- can return a value. The difference between a function and a sub is that a function can return a value, but a sub cant return any value.

Passing Parameters in a Function You can pass parameters in a function the same way its done for a sub. Lets see an example for this. Sample Code 3: Using Parameters in a Function 1 Dim v1, v2 2 v1=5 : v2=10 fnSum v1, v2 'Passing parameters using variables 3 fnSum 10, 20 'Passing parameters as literals 4 fnSum v1+10, v2*2 'Passing parameters as expressions 5 'Function Definition 6 Function fnSum(var1, var2) 7 sum = var1 + var2 8 msgbox "The sum of numbers is: " & sum 9 End Function 10

Returning a value from a Function in QTP One additional advantage that Functions have over Sub is that a Function can return a value. To return a value from a function, we need to take care of the following two things 1) To return a value from a function, you need to use the statementfunctionName = ReturnValue, where functionName is the actual name of the function and ReturnValue is the value you want to return. 2) To capture the returned value, you need to use the statement someVariable =functionName() while calling the function. Lets understand this with the help of an example.

Sample Code 4: Returning value from a Function Dim result 'variable that will capture the result 1 2 result = fnSum(10, 20) 'parameters should be passed using parenthesis when 3 value 4 msgbox "The sum of the numbers is: " & result 5 6 'Function Definition 7 Function fnSum(var1, var2) 8 sum = var1 + var2 9 'return the result 10 fnSum = sum 11 End Function

Passing Parameters to a Function/Sub Pass By Value & Pass By Reference

You can pass parameters to a function or sub procedure by value or by reference. Lets see what both these terms mean. Passing Parameters by Value (byVal). With this way of passing parameters, only a copy of the original parameters is passed. This means that whatever modifications we make to the parameters inside the function, it doesnt affect the original parameters. Sample Code 5: Passing parameters by value to a function 1 Dim val 2 val=5 3 4 'Function Call fnFunc val 5 msgbox "Original Value: " & val 'msgbox displays value 5 6 7 'Function Definition 8 Function fnFunc(byVal val) 9 val = val + 2 10 msgbox "New Value: " & val 'msgbox displays value 7 11 End Function 12 In the above example you would see that the new value get changed to 7 but it doesnt get reflected to the original value which still shows the value as 5 only. Passing Parameters by Reference (byRef). In this case, the reference of the original value is passed as the parameter to the function. Therefore, whatever change is done to the parameter inside the function, the same is reflected in the original parameter also. By default, values are passed by reference in a function. i.e., even if you dont use byRef keyword, the parameters are passed by reference. Sample Code 6: Passing parameters by reference to a function 1 Dim val 2 val=5 3 'Function Call fnFunc val 4 msgbox "Original Value: " & val 'msgbox displays value 7 5 'Function Definition 6 Function fnFunc(ByRef val) 7 val = val + 2 8 msgbox "New Value: " & val 'msgbox displays value 7 9 End Function 10 Since the original parameter is passed as reference to the function, both the original and new value has the updated value 7.

You might also like