site
stats
By Anish Pillai Anish Pillai Posted under qtp and vbscript tutorials | QTP Tutorials for Beginners

Part 7: QTP and VBScript | Writing Sub Procedures & Functions in QTP

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

Many a times when you are writing your test scripts, you might encounter some lines of code that need to be executed multiple times during the course of the script. Rather than repeating the same code in your script multiple number of times, the common practice is to create a function for it and call the function repeatedly. Let’s see how you can do this in QTP. This article would cover the following concepts –

  • — What are procedures in VBScript and how they are used
  • — What are Subs in QTP
  • — Examples of how 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

Let’s 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. We’ll 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

Let’s 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) Sub Definition - Actual Code
Sub fnSum()
  var1 = 5 : var2 = 10
  sum = var1 + var2
  msgbox "The Sum of Numbers is: " & sum
End Sub

'2) Executing Sub
Call fnSum() 'Call keyword is not mandatory to execute a Sub
fnSum()

'Structure for Function
'======================

'1) Function Definition - Actual Code
Function fnSum()
  var1 = 5 : var2 = 10
  sum = var1 + var2
  msgbox "The Sum of Numbers is: " & sum
End Function

'2) Executing Function
Call fnSum() 'Call keyword is not mandatory to execute a Function
fnSum()

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 Sub statements.
  • – 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. Let’s see an example for the same.

Sample Code 2: Using Parameters in a Sub

Dim v1, v2
v1=5 : v2=10

fnSum v1, v2 'Passing parameters using variables
fnSum 10, 20 'Passing parameters as literals
fnSum v1+10, v2*2 'Passing parameters as expression

'Sub Definition
Sub fnSum(var1, var2)
  sum = var1 + var2
  msgbox "The sum of numbers is: " & sum
End Sub


Functions in QTP

A Function in QTP –

  • – is a set of statements that are enclosed between Function and End Function statements.
  • – 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 can’t return any value.

Passing Parameters in a Function

You can pass parameters in a function the same way it’s done for a sub. Let’s see an example for this.
Sample Code 3: Using Parameters in a Function

Dim v1, v2
v1=5 : v2=10
fnSum v1, v2 'Passing parameters using variables
fnSum 10, 20 'Passing parameters as literals
fnSum v1+10, v2*2 'Passing parameters as expressions
'Function Definition
Function fnSum(var1, var2)
  sum = var1 + var2
  msgbox "The sum of numbers is: " & sum
End Function


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 statement functionName = 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.

Let’s understand this with the help of an example.

Sample Code 4: Returning value from a Function

Dim result 'variable that will capture the result

result = fnSum(10, 20) 'parameters should be passed using parenthesis when the function returns a value
msgbox "The sum of the numbers is:  " & result

'Function Definition
Function fnSum(var1, var2)
  sum = var1 + var2
  'return the result
  fnSum = sum
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. Let’s 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 doesn’t affect the original parameters.


Sample Code 5: Passing parameters by value to a function

Dim val
val=5

'Function Call
fnFunc val
msgbox "Original Value: " & val 'msgbox displays value 5

'Function Definition
Function fnFunc(byVal val)
  val = val + 2
  msgbox "New Value: " & val 'msgbox displays value 7
End Function

In the above example you would see that the new value get changed to 7 but it doesn’t 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 don’t use byRef keyword, the parameters are passed by reference.


Sample Code 6: Passing parameters by reference to a function

Dim val
val=5
'Function Call
fnFunc val
msgbox "Original Value: " & val 'msgbox displays value 7
'Function Definition
Function fnFunc(ByRef val)
  val = val + 2
  msgbox "New Value: " & val 'msgbox displays value 7
End Function

Since the original parameter is passed as reference to the function, both the original and new value has the updated value 7.

This was all about Functions and Sub Procedures in QTP. Please feel free to provide your feedback for this article and the entire QTP VBScript tutorial series. You can also leave a comment if you would like any other details to be covered here.

If you enjoyed this article, you can join our blog to get new articles delivered directly in your inbox.

To check out more tutorials, visit our QTP Tutorials page. You can also check the Archives page to view the list of all our articles.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • Kris

    Really great & short & sweet explanation about by Val & by Ref passing of parameters..!

    Thanks a lot anish..!

    • Anish10110

      Thanks Kris.. πŸ™‚

  • Shalini

    I am learning a lot .. Thanks

    • Anish10110

      Thank you Shalini.. πŸ™‚ Keep visiting and do provide your feedback and suggestions on different articles. This will help us a lot in improving the content.

  • Keerti

    Anish in the sample function which you have written has a small error if am not wrong.The sum gives the concatenated result.
    Its better we have sum=int(var1)+int(var2)…correct me if am wrong.

    • Anish10110

      Keerti, Which function are you referring to here? I have checked the code and for me all functions are working fine. But as you pointed out using Int(variable name) is a good idea when we are passing variables. Because it will concatenate the values if the numbers are taken as strings. But since I had directly taken integer values here. So I didn't take Int with variable names.

      • Keerti

        Hi Anish,
        Actually when i tried executing the sample code 1 both function and sub procedure,i found the result to be concatenated.I was wondering for the same as you had used integers directly.Still tried including INT(variable name),which when used gave me right result.
        have started going through your posts from today,and for your information am very much into learning upcoming topics..Will get back to you for any doubts.

      • Keerti

        While executing the sample code 1 ,i had made small changes,….To be specific, I used prompts for both the variables ,and that was the reason which gave me concatenated result even when i gave digits as i/p.So finnally made me use int(var).

        It was my mistake n nt in your code.

    • Balaraju

      as per my knowledg above thhing is wrong we shuld like this is better "sum=cint(var1)+cint(var2)

  • santosh

    nice

  • ramesh

    thank you for providing detailed information

  • Ram

    What is the password for VBScript_in_QTP

    • Anish10110

      Which password are you looking for?? I'm not sure if there is a password here in this article.

  • Nag

    too cool.. thanks BOSS

  • MEE

    awesome…….

  • MEEq

    nice

    • Anish10110

      Thanks.. πŸ™‚

  • Santhosh

    Hi Anish,

    Your site is just great buddy!!! You are the best automation trainer I would say.

    I had one question on Sub and Function. what is the benefit of having Sub when we have Function? Sub cannot return a value. We can always use functions and whether to return a value from the function, we can always decide for that matter .

    What say buddy?

    Thanks
    Santhosh

    • Anish10110

      Even I think that functions will provide all the solutions.. Maybe since qtp uses vbscript, so it provides sub as an option.. I have myself never used sub.. I always use functions only..

    • Surya

      Hi Santhosh,

      The benefit of using sub is that it is faster in execution than functions as functions allocate additional memory slots in order to return values.

  • klakston

    It's really useful for everyone…Super

  • Neeraj

    Crisp and presize…Very Good tutorial..It is very useful and informative

  • Prashant

    What is default type of passing values in vb script?

  • Raj

    Thanks for explaining nicely..:)

  • Jhan

    Very nice explanation

  • sachin

    Good Explaination

  • Ravindra Sharma

    Anish,
    You are really a great trainer, we all are privileged to have a such great portal in place.!!

  • Mumthaj

    Hi Anish ,
    am new to vbscript
    could you tell me difference between the functions and function procedures . are they same

  • Narayanan

    Even a kid can understand if he read this article.
    Thanks for detailed explanation

  • Shuchi Gupta

    Hi Anish,

    I am
    really grateful to you,

    I m a
    person who don’t like coding at all…

    But
    your tutorials are very simple and easy to understand, Never thought that I’ll
    be able to learn any language,

    Thanks
    again for explaining it in such a simple and effective manner..

    God
    bless you.

    πŸ™‚

  • Jaggu

    Thank you Anish.. Its very useful and even a layman like me can understand the concepts..

  • shyam

    thank you very much

  • Anjali

    nice

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×