WORKING WITH QTP

TestEveryThinG

Importance of Parenthes when we call Function or Sub QTP

Posted by rajivkumarnandvani on May 3, 2009

Whenever you define a function with using argument always define the argument type by using ByVal OR ByRef Because if u not define the argument type by default it take ByRef

Parenthesis matter in QTP (and VBScript). And they surely make a difference. During debugging a function call. There was different behaviour between these two function calls:
foo(bar)
call foo(bar)
Passing an argument to a function surrounded by parenthesis means: “Protect me” or in other words: treat me as byVal even if it is defined in the function as byRef.

Example:
sub samplesub (a, b) ‘ a and b are by default handled as byRef
a = a + b
end sub

And this is happening when we call samplesub:
x = 1
y = 2
z = 4
samplesub x, y
samplesub (z), y
msgbox x ‘ displays “3”
msgbox z ‘ displays “4” because z was passed as if it was passed byVal

The same applies when you call a function:

function samplefunc(c)
c = c^2-1
samplefunc = (c mod 2 = 1)
end function

q = 8
samplefunc q
msgbox q ‘ returns 63

‘ When you accidentally forgot to call:
p = 9
samplefunc(p)
msgbox p ‘ returns 9, because p is returned byVal

‘ With call:
r = 10
call samplefunc(r)
msgbox r ‘ returns 99, because r is returned byRef

‘ With call and argument protected:
s = 11
call samplefunc( (s) )
msgbox s ‘ returns 11, s is returned byVal

‘ And a last example of a function call with multiple argument with combined protection:
call multifunc( (IamProtected), IamUnprotected )

Rules in short:
A sub/function call with an argument in protected mode overrides a byRef in the function.
A sub/function call with an argument in unprotected mode is returned byRef by default unless it is overridden in the function by a byVal.
An literal or const is always returned byVal.

Syntax proposal:
OK, it is ugly, but if you use parenthesis because they are part of the call, you should use them with spaces between the first and last argument and no space between the function:

call f( a, b )

If you want to use arguments in protected mode, you should use no spaces between the parenthesis and the arguments, but do use them between the function/sub and the parenthesis belonging to the function/sub call:

f (a), (b)
or
call f( (a), (b) )

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: