site
stats
By Anish Pillai Anish Pillai Posted under QTP Concepts

Difference between Call Statement and normal Function Call in QTP

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
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. i.e. 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.

If you enjoyed this article, you can join our blog to get free email updates directly in your inbox.

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×
  • Pingback: QTP & VBScript | Writing Functions and Sub Procedures in QTP - Automation Repository()

  • mallikarjuna

    Thanks,for your helping

    • Anish10110

      Thank you.. 🙂 Keep visiting..

  • Manoj

    Good one, It helped.

  • Srinivas Nandiraju

    Hi

    In the example given

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

    the second line should be fnReturnsVal= “this string is returned back” ? I mean is there a typo?

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×