Sunday, 24 November 2013

Differnce between Sub and Functions in QTP


1. QTP Actions:-

Action is specific to QTP and not the part of vbscript. Every QTP test has at least one Action(default name is Action1).
Action can have an object repository associated with it. Action can return multiple values in form of 'output parameters'.

2. Procedures:
2.1. VBScript Sub Procedures:-

A Sub procedure:

    * is a series of statements, enclosed by the Sub and End Sub statements
    * can perform actions, but does not return a value
    * can take arguments
    * without arguments, it must include an empty set of parentheses ()


Sub mysub()
  Print "my Sub Procedude"
End Sub

or

Sub mysub(argument1,argument2)
  Print "my Sub Procedure"
End Sub

How to call Sub Procedures:

To call a Sub, you will use Call statement by enclosing arguments (if any) in parentheses.


The Call statement is not necessary to call a Sub, but if you want to use Call statement (Recommended), you must enclose arguments (if any) in parentheses.

Call mysub(argument1,argument2)

You can call a Sub without using Call statement as well, but not recommended.

mysub argument1,argument2


2.2. VBScript Function Procedures:-

A Function procedure:

    * is a series of statements, enclosed by the Function and End Function statements
    * can perform operations and can return a value
    * can take arguments that are passed to it by a calling procedure
    * without arguments, must include an empty set of parentheses ()
    * returns a value by assigning a value to function name itself


Function myfunction1()
  Print "my fuction1"
End Function

or

Function myfunction2(a,b)
myfunction2=a+b  'assign value to function name
End Function


How to call Function Procedures:

Call myfunction1() 'calling 1st function, without any return value

abc=myfunction2(argument1,argument2)  'calling 2nd function, with a return value

Here you call a Function called "myfunction2", the Function returns a value that will be stored in the variable "abc".

Hope all your confusions are gone! But if you still have any doubts, please post your comments!


1 comment:

  1. i cant understand the differnce.can you please elaborate?

    ReplyDelete