17 Jan 2013

Difference between Call Statement and Normal Function Call in QTP


Difference between Call Statement and normal Function Call in QTP

Call is a VBScript Statement that transfers control to a Sub or Function procedure.

The syntax of Call statement is:   [Call] functionOrSubName [Argument(s)]

where, Call is the optional keyword,

functionOrSubName is the name of the function or sub to be called,

Argument(s) is the comma-delimited list of parameters.

Hence to call a function in QTP, we can write ‘fnFunction()’ or ‘Call fnFunction()‘. Lets see

what are the differences between calling a function with Call statement and without Call

statement.We’ll check the differences between ‘fnFunction()’ and ‘Call fnFunction()’ based on the

following 3 points

a) Case where the function to be called neither has any parameters nor does it return any value:

In this case, both ‘Call fnFunction()’ & ‘fnFunction()’ behave in the same manner.

Example:

‘—-Function Definition

Function fnNoParam_NoReturnVal()
msgbox “No Parameters & No Return Values”
End Function

‘—-Function Call

fnNoParam_NoReturnVal()
Call fnNoParam_NoReturnVal()

In this example, both the function calls behave in the same manner and display the text “No

Parameters & No Return Values” in a message box.

b) Case where the function to be called has arguments but doesn’t return any value:

In a normal function call, the user can pass the parameters with and without parentheses.

both ‘fnFunction(param)’ & ‘fnFunction param’ would work. But if Call statement is used here,

parentheses must be provided while passing the arguments, else QTP would throw a run-time

error.

Example:

‘—-Function Definition

Function fnNoReturnVal(param)
    msgbox param
End Function

‘—-Function Call

fnNoReturnVal(“some parameter”)  ‘displays message box
fnNoReturnVal “some parameter”  ‘displays message box

Call fnNoReturnVal(“some parameter”)  ‘displays message box
‘Call fnNoReturnVal “some parameter”  ‘runtime error

c) Case where function to be called returns a value:

In this case, the normal function call (without call statement) can capture the returned value. But

if we use Call statement here, the function’s return value can’t be captured.

Example:

‘—-Function Definition

Function fnReturnsVal()
fnReturnsValue = “this string is returned back”
End Function

‘—-Function Call

str1 = fnReturnsVal() ‘returns value to str1 variable
‘str2 = Call fnReturnsVal() ‘runtime error  

In this example, ‘str2 = Call fnReturnsVal()’ throws a runtime error as Call statement doesn’t allow us to capture function’s returned value.

No comments:

Post a Comment