.

HP QTP Quiz: Passing by value vs Passing by reference

Here is a QTP quiz for you. We have two functions below. For the first one, argument is passed by reference while for the second, argument is passed by value.

A. Argument is passed by Reference

Function learnqtp( ByRef var)

    myname= "jain"
    msgbox var
    msgbox myname

End Function

myname= "ankur"

call learnqtp (myname)

B. Argument is passed by Value:

Function learnqtp( ByVal var)

    myname= "jain"
    msgbox var
    msgbox myname

End Function

myname= "ankur"

call learnqtp (myname)

Without copying the functions in your QTP editor, try to find:

  1. Output of A.
  2. Output of B.

Why do you think output differ/doesn’t differ? Give your answers in comment below. Now, open QTP and check your answers. Any more comments? :)

Technorati Tags: ,,,

Have you downloaded the FREE Optimizing QTP eBook yet? Get It Now!

If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

21 comments ↓

#1 Percy Bell on 11.03.08 at 07:17

A:
jain
jain

B:
ankur
jain

Passing by Value is just that. It passes a value to the function to use. It creates a new space to hold this value.

Passing by Reference passes the pointer to the memory space for the variable. This means that anything you do to the variable in the function changes the actual value of the passed variable. This means that if you changed the variable in the function it will be changed outside the function too.

#2 Zecty on 11.03.08 at 10:11

The output will be different. The first code (byRef), will output the name “jain” twice. Because, when a variable is passed by reference, both the variable “var” (passed as a parameter) and “myname” (used inside the function) will point to the same memory space. Since “myname” was not declared (“dim”) inside the function, when a variable is passed by reference in vbscript, use of the variable name refers to the calling scope’s use of the variable. But when passed by value (byVal), the “myname” variable used inside the function and the “var” parameter, will both be ‘local variables’ that are instantiated only for the life of the function, and do not affect the memory area used by the ‘global’ “myname” variable.

This shows why it is a good reason to always declare variables!

#3 Ankur on 11.05.08 at 05:37

Thanks Percy Bell and Zecty. Nice explanation.

#4 avinash on 12.18.08 at 03:16

good……………….

#5 kiran on 12.22.08 at 04:33

Perfect solution provided. Great !!!

#6 srikanth hebbar on 01.30.09 at 22:43

In the scripting we can call the functions to perform to some activities, in that we can pass the information from main to function part by two ways. one is by passing the values and another one by passing the address of that value by using pointers.

#7 Ren on 02.03.09 at 23:05

Very nice Explanation

#8 Uma on 04.10.09 at 04:49

Great articles.Thank you

Uma

#9 Ankur on 04.10.09 at 07:56

Thanks Uma

#10 Rammy on 08.11.09 at 14:09

in the 2nd Function, myname=”ankur” is writted outside the Function, how come this value will display, once we run it..

#11 rahul_tester on 09.30.09 at 11:03

I have a question. If instead of calling the functions in line i.e. creating an external library file, i use the same code the output is different. Can some one explain why?

#12 Nataraj N G on 11.13.09 at 18:20

Hello All,
1. In a script1 i want to get value from the application, how to get value of any field from application?
2. In script2 i want to use the value that i get from the application?

Please let me know, how to do get value from script1
& how to use the value in script2?

#13 Nataraj N G on 11.13.09 at 18:20

Hello All,
1. In a script1 i want to get value from the application, how to get value of any field from application?
2. In script2 i want to use the value that i get from the application?

Please let me know, how to do get value from script1
& how to use the value in script2?
Note: without using of ouput value & Datatable.

#14 Rajasekhar Reddy on 03.17.10 at 07:52

Hello All,
Its Nice Explanation……..

#15 ram on 03.23.10 at 16:44

nice one

#16 H@ri$h on 05.11.10 at 12:26

Hi this was good exploratory explanation

#17 Madhu on 05.27.10 at 02:21

Hi,
When i am trying to add a new column in global sheet using below code–>
DataTable.GetSheet(“dtGlobalSheet”).AddParameter “NewColumn”,”hello”

its throwing an error as “The DataTable.GetSheet operation failed. The dtglobalsheet sheet does not exist.”
***Can anyone give me a solution****

Cheers,
Madhu

#18 mahesh on 06.04.10 at 21:58

very nice explanation

#19 Tamiz on 06.17.10 at 18:16

Explanation is good…

#20 Jacky on 06.30.10 at 18:35

Madhu,

This will work

Datatable.GetSheet(“Global”).AddParameter “New Column”,”Hello”

#21 sekhar on 07.15.10 at 16:54

Hi Madhu,

please remove double quotes for dtGlobalSheet and try the same statement.

it works..

DataTable.GetSheet(dtGlobalSheet).AddParameter “NewColumn”,”hello”

Leave a Comment