Pages

Saturday, February 16

Difference between Action, Procedure, Function and Sub?

Hello Friends!

I believe that many QTP newbie’s are confused about Action, Procedures, Function and Subs. Am I right?

No worries! Here we are going to discuss all of them in detail to eliminate all the confusions!

In QTP, there are two ways (on broad level); we can break up the code into logical units. Logical unit is nothing but a 'Piece of code' or ‘a series of VBScript statements’, to perform specific activity in QTP. These two ways are -

1. Actions - specific to QTP
2. Procedures - vbscript provided


Okie.. but what about Functions and Subs? Wait guys! we'll come there.

We know that we use vbscript as scripting language in QTP. (To read about vbscript, Click Here>> )

VBScript has two kinds of procedures: Sub procedure and Function procedure

So, in brief, its goes like this..


1. Actions
2. Procedures
   2.1. Subs/Subroutine
   2.2. Function

Now we know that Action and Procedures are the two things. Procedure is further devided in two types - Function & Sub.

Feeling batter? Great!!

So, Lets start with actions..

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!

31 comments:

  1. it's relly very helpful. thanx a lot!!

    ReplyDelete
  2. Hi Abhikansh,
    I went through your post. Does it mean that to return multiple vales from the lines of code, we should use action cos function can return only one value and subprocedures cant return a value.

    ReplyDelete
    Replies
    1. Hi Mavrick,
      It's not necessity to use actions...in indirectly u can return multiple values by passing parameters byref....Ram

      Delete
  3. @maverick

    You can either use actions or function to return multiple value.. but in case of function, you need to store all the output values to an array and return that array, bcoz function can return only one value. Refer the below code..

    Function myfunction2(a,b)
    val_sum = a+b
    val_sub = a-b
    val_div = a/b
    arr=Array(val_sum,val_sub,val_div)
    myfunction2=arr 'assign value to function name
    End Function

    x= myfunction2(15,3)

    msgbox x(0)
    msgbox x(1)
    msgbox x(2)

    ReplyDelete
    Replies
    1. You can also return multiple values in function just by declaring in function definition..
      for Example,

      Function samplefunciton(input1, input2, output1, output2)

      output1 = input1 + input2
      output2 = input1 * input2

      End Function

      num1 = 2
      num2 = 4

      Call Function samplefunciton(num1, num2, addnum, multnum)

      In above example, addnum will store/return 6 and multnum with store/return 8

      Delete
    2. this code is not working

      Delete
    3. Hi maverick,
      It is good example to return multiple values at a time from calling function

      Regards,
      Sree

      Delete
  4. very nice. Thank u

    ReplyDelete
  5. Great piece of info! Thanks!

    ReplyDelete
  6. Great work.. to clear confusion

    ReplyDelete
  7. thank u for info ..its very usefull.

    ReplyDelete
  8. hi..clarified.. Thank you

    ReplyDelete
  9. Can u plz explain where to use sub procedure and where to function procedure in real time automation testing ??

    ReplyDelete
  10. its depend on the what you are trying to achieve from function.. There is no standard rule..

    ReplyDelete
  11. Nice Explanation . Good Job. Keep it Up.

    ReplyDelete
  12. I need to know when to use actions and when to use functions real time. Please explain.

    ReplyDelete
  13. could you explain me
    we can call a Sub without using Call statement. and what about function call?

    ReplyDelete
  14. gr8 info...........:-)

    ReplyDelete
  15. very nice..Thank u :)

    ReplyDelete
  16. I have a message dialog in QTP with HTML string that displayed over few rows (dynamic, don't know in advance how many rows will be). As a part of each row, there is a date string.
    For example,
    Hello 02/02/2013 bla,bal
    Hello 03/04/2013 bla1 bal2
    Hello 04/04/2013 bla3 bla4
    I need to find (output) each date in that string and then validate it. If it was just 3 rows - would be easy but it is a html string. Any ideas?

    ReplyDelete
    Replies
    1. strMsg = "Hello 02/02/2013 bla,bal" & vbcrlf & "Hello 03/04/2013 bla1 bal2" & vbcrlf & "Hello 04/04/2013 bla3 bla4"
      Msgbox(strMsg) ' Just to check the source string

      arrMsg = split(strMsg,vbcrlf) ' Split value in array with the delimiter crlf ( carriage return and line feed)

      ' check the upper bound of an array if there are exact 3 values (Array start with 0 to 2 (3 values))
      if (ubound(arrMsg)) = 2 then

      'Loop through all the rows (lower bound to upper bound)
      for cntMsg = lbound(arrMsg) to ubound(arrMsg)

      ' Again split a single row with the space delimiter and get the message of the 2nd position of an array (in this example 2nd is Date value)
      Msgbox split(arrMsg(cntMsg)," ")(1)
      next
      else
      Msgbox("Not 3 rows")
      end if

      Delete
  17. example for actions ? can any one tellplease ?

    ReplyDelete
  18. what are the functions in qtp and actions asked by value labs in interview please help

    ReplyDelete
    Replies
    1. There are 3 types of Action in QTP
      1.Reusable Action
      2.Non- Reusable Actions
      3.Exterinal Actions


      2 types of functions
      1. Sup Procedures
      2. Function Procedures

      Delete
  19. how to return value from a sub routine?
    Any Help Appreciated.

    ReplyDelete
  20. return value for sub routine is impossible, only function can return.

    ReplyDelete