17 May 2013

Difference between Function and Sub Procedure


Sub Procedure

nothing but collection of statements that are enclosed between Sub and End Sub statements.
can be used both with and without parameters.
does not return any value.

Passing Parameters in a Sub Procedure

Consider a situation where you want to run the same set of code multiple times but with different set of data. In such a case, you can pass the data to your Sub in the form of parameters. 

This way you can make the code generic which can work with multiple set of data. 

Let’s see an example for the same.

Using Parameters in a Sub

Dim a,b
fnSum a, b'Passing parameters using variables
fnSum 10, 20 'Passing parameters as literals
fnSum a+10, b*2 'Passing parameters as expression

'Sub Definition
Sub fnSum(var1, var2)
  sum = var1 + var2
  msgbox "The sum of numbers is: " & sum
End Sub

Function

nothing but a set of statements that are enclosed between Function and End Function statements.
can be used with and without parameters
can return a value.

note:The difference between a function and a sub is that a function can return a value, but a sub can’t return any value.

Passing Parameters in a Function

Consider a situation where you want to run the same set of code multiple times but with different set of data. In such a case, you can pass the data to your Sub in the form of parameters. 

This way you can make the code generic which can work with multiple set of data. 

Let’s see an example for this.

Using Parameters in a Function

Dim a, b
a=5 
b=10
fnSum a, b'Passing parameters using variables
fnSum 10, 20 'Passing parameters as literals
fnSum a+10, b*2 'Passing parameters as expressions
'Function Definition
Function fnSum(var1, var2)
  sum = var1 + var2
  msgbox "The sum of numbers is: " & sum
End Function

Returning a value from a Function in QTP

One additional advantage that Functions have over Sub is that a Function can return a value. To return a value from a function, we need to take care of the following two things 

To return a value from a function, you need to use the statement functionName = ReturnValue, where functionName is the actual name of the function and ReturnValue is the value you want to return.

To capture the returned value, you need to use the statement someVariable = functionName() while 

calling the function.

Let’s understand this with the help of an example.

Returning value from a Function

Dim result 'variable that will capture the result

result = fnSum(10, 20) 'parameters should be passed using parenthesis when the function returns a value
msgbox "The sum of the numbers is:  " & result

'Function Definition
Function fnSum(var1, var2)
  sum = var1 + var2
  'return the result
  fnSum = sum
End Function

Passing Parameters to a Function/Sub – Pass By Value & Pass By Reference

You can pass parameters to a function or sub procedure by value or by reference. 

Let’s see what both these terms mean.

Passing Parameters by Value (byVal). 

With this way of passing parameters, only a copy of the original parameters is passed. This means that whatever modifications we make to the parameters inside the function, it doesn’t affect the original parameters.

Passing parameters by value to a function

Dim val
val=5
'Function Call
fnFunc val
msgbox "Original Value: " & val 'msgbox displays value 5

'Function Definition
Function fnFunc(byVal val)
  val = val + 2
  msgbox "New Value: " & val 'msgbox displays value 7
End Function

In the above example you would see that the new value get changed to 7 but it doesn’t get reflected to the original value which still shows the value as 5 only

Passing Parameters by Reference (byRef)

In this case, the reference of the original value is passed as the parameter to the function. Therefore, whatever change is done to the parameter inside the function, the same is reflected in the original parameter also. By default, values are passed by reference in a function. i.e., even if you don’t use byRef keyword, the parameters are passed by reference.

Passing parameters by reference to a function

Dim val
val=5
'Function Call
fnFunc val
msgbox "Original Value: " & val 'msgbox displays value 7
'Function Definition
Function fnFunc(ByRef val)
  val = val + 2
  msgbox "New Value: " & val 'msgbox displays value 7
End Function

Since the original parameter is passed as reference to the function, both the original and new value has the updated value 7

No comments:

Post a Comment