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:
- Output of A.
- 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: hp qtp,qtp quiz,by value,by reference
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 ↓
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.
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!
Thanks Percy Bell and Zecty. Nice explanation.
good……………….
Perfect solution provided. Great !!!
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.
Very nice Explanation
Great articles.Thank you
Uma
Thanks Uma
in the 2nd Function, myname=”ankur” is writted outside the Function, how come this value will display, once we run it..
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?
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?
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.
Hello All,
Its Nice Explanation……..
nice one
Hi this was good exploratory explanation
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
very nice explanation
Explanation is good…
Madhu,
This will work
Datatable.GetSheet(“Global”).AddParameter “New Column”,”Hello”
Hi Madhu,
please remove double quotes for dtGlobalSheet and try the same statement.
it works..
DataTable.GetSheet(dtGlobalSheet).AddParameter “NewColumn”,”hello”
Leave a Comment