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? 🙂
If you want to keep track of further articles on UFT (QTP). I recommend you to subscribe by email below and have new UFT articles sent directly to your inbox.
suresh says
Hi Ankur,
1)how to retrieve the values from GRID…?
2nd question, but qtp recognizes that is not a grid , that is a web element? what u do?
pls post ans its urgent
thanks in advance
Veeranki Naveen says
Hi Ankur,
The above examples of byref holds true when the Function “Definition” and Function “Call” are in the same Action.
But when you define the same Function in a function library file and call that function from the main Action then the above example doesn’t fit true. Can you please explain why is it so ?
For Example :
I have created a Function “ad” in Function Library as below and also declared the variables p, q globally and I associated the function library “Globally” from Test settings Resource tab:
FUNCTION LIBRARY
Dim p , q
Function ad (byref a , byref b)
msgbox p
p = (p+1)
msgbox a ‘ OUT PUT 3
c = (a+b)
a = (a+1)
msgbox c
End Function
Now I assigned the values to variable p and q in the main Action and also called the function “ad” as below .
ACTION1
p = 3
q = 5
Call (p,q)
msgbox p
QUESTION 1 :
Now when I am incrementig ‘p’ by 1 inside the function it is not reflecting in ‘a’ even though ‘a’ is a reference of ‘p’ but the otherway is happening i.e ‘p’ is reflecting the changes done in ‘a’.
Can you please explain why is it so ?
QUESTION 2 :
What if I declare the variables in an Action rather than declaring them inside a FUNCTION LIBRARY ?
Veeranki Naveen says
I have a question. If instead of calling the functions in line i.e (Action1 contains both Function Call and Function Definition )
but if we create an external library file for function definition and call the function from Action1 and use the same code the output is different. Can some one explain why?
This question has already been asked by Rahul_Tester but no reply ?
ravi says
Need simple example for Call by value and Ref.
Bandi sekhara reddy says
Hi Folks,
How to working with the Recovery Scenarios in order to Handling Run time Un Expected Popups,
Bandi sekhara reddy says
HI Ankur,
If I have the One Action , Parameterizing the Test Data (Excel Sheels) to the Action,While Executing the 20 times with different Inputs Parameters How could we store the Results of 20 Iteration of Action Pass or Fail in the Runtime Without seeing the QTP Results .
Its a Great Help full,,
Suhas says
Hi,
In QTP Function Which is Default Type. Pass By Val or PassBy Reff?
Bandi sekhara reddy says
Really Good Examples,, It would great help full to understand the Full information about By Val, By Ref..
Manish Ranjan says
Good Example
ravinder says
@uday call by reference is the default option in vbscript
uday says
Qtp by default uses pass by value or reference ?
Sagar Mitkari says
Hi Ankur i ddnt got the point “Zecty” have mentioned in last line of his comment.
“This shows why it is a good reason to always declare variables!
”
What dose this means??
anurag wani says
Hi Ankur, is there any way to pass the datatable object as function argument, actually I am facing a problem with my code that I am calling a function from .vbs and trying to access the global data datatable value inside it. but not able to do so….hence I thought if I can pass data table as argument, I need not to pass required parameters and can access them inside the function…plz help and correct if I am wrong…thanks in advance
sekhar says
Hi Madhu,
please remove double quotes for dtGlobalSheet and try the same statement.
it works..
DataTable.GetSheet(dtGlobalSheet).AddParameter “NewColumn”,”hello”
Jacky says
Madhu,
This will work
Datatable.GetSheet(“Global”).AddParameter “New Column”,”Hello”
Tamiz says
Explanation is good…
mahesh says
very nice explanation
Madhu says
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
H@ri$h says
Hi this was good exploratory explanation
ram says
nice one
Rajasekhar Reddy says
Hello All,
Its Nice Explanation……..
Nataraj N G says
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.
Nataraj N G says
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?
rahul_tester says
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?
Rammy says
in the 2nd Function, myname=”ankur” is writted outside the Function, how come this value will display, once we run it..
Uma says
Great articles.Thank you
Uma
Ankur says
Thanks Uma
Ren says
Very nice Explanation
srikanth hebbar says
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.
kiran says
Perfect solution provided. Great !!!
avinash says
good……………….
Ankur says
Thanks Percy Bell and Zecty. Nice explanation.
Zecty says
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!
Percy Bell says
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.