Tuesday, May 14, 2013

Pass By Value vs Pass By Reference


We can pass parameters to a function or sub procedure by value or by reference. 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.

What is the difference between pass by reference and pass by value?

Pass by value calls a function or returns a value based on the “number” or "value". Maybe you're calling a function with the value of 5, you would just call the function passing the 5. The function can't change the 5, it's always a 5.
Pass by reference calls a function and passes a pointer to a memory location that contains the value. You might call the function pointing to a location that contains the number of people who live in your home. That location has been populated with a value, so the function can look at that location and determine that yes, there are 5 members in your family. But the function may be responsible for updating the number of people in your family and you just had a baby, so the function would update the value that is stored at the location. Pass by reference just gives a "pointer" to the memory location.

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.

Sample Code: 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.

Sample Code: 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.


Let us take another Example to understand.

' Arguments passing to functions
'  Pass ByRef
' Pass ByVal
'**********************************************************************
' Function name : Demo_PassingArgs1
' Variables: num1 , num2  --> both are declared as ByRef  (BY DEFAULT)
' Passed  arguments a,b  :  a=10, b=20
' Returning values :    a=100, b=200
' because a=num1, b=num2 ; both these values are passed as ByRef
'**********************************************************************
a=10
b=20
msgbox Demo_PassingArgs1(a,b)
msgbox a
msgbox b

Function Demo_PassingArgs1(num1,num2)
   msgbox num1 ' Value of a
   msgbox num2 ' value of b
   num1=100
   num2=200
   Demo_PassingArgs1=num1+num2
End Function
'**********************************************************************
' Function name: Demo_PassingArgs2
' Variables:   num1 , num2  --> num1 and num2 are declared as ByRef 
' Passed  arguements a,b: a=10, b=20
' Returing values :  a=100, b=200
' because a=num1, b=num2 ; both these values are passed as ByRef
'*********************************************************************
a=10
b=20
msgbox Demo_PassingArgs2(a,b)
msgbox a
msgbox b

Function Demo_PassingArgs2(ByRef num1,ByRef num2)
   msgbox num1 ' Value of a
   msgbox num2 ' value of b
   num1=100
   num2=200
   Demo_PassingArgs2=num1+num2
End Function
'****************************************************************************
'Function name : Demo_PassingArgs3
' Variables: num1,num2;both are declared as ByVal(declared in function)
' Passed  arguements a,b      : a=10, b=20
' Returing values :   a=10, b=20
' because a=num1, b=num2 ; both these values are passed as ByVal
'*******************************************************************
a=10
b=20
msgbox Demo_PassingArgs3(a,b)
msgbox a
msgbox b

Function Demo_PassingArgs3(ByVal num1,ByVal num2)
   msgbox num1 ' Value of a
   msgbox num2 ' value of b
   num1=100
   num2=200
    Demo_PassingArgs3=num1+num2
End Function

'********************************************************************
' Function name   :   Demo_PassingArgs4
' Variables :num1,num2; num1 declared as ByRef;num2 declared as ByVal
' Passed  arguements a,b  :  a=10, b=20
' Returing values :   a=100, b=20
' because a=num1 (here num1 is ByRef) , b=num2  (here num2 is ByVal)
'*********************************************************************
a=10
b=20
msgbox Demo_PassingArgs4(a,b)
msgbox a
msgbox b

Function Demo_PassingArgs4(ByRef num1,ByVal num2)
   msgbox num1 ' Value of a
   msgbox num2 ' value of b
   num1=100
   num2=200
   Demo_PassingArgs4=num1+num2

End Function

Err Object

The Err object contains information about run-time errors.
Properties
Description Property
HelpContext Property
HelpFile Property
Number Property
Source Property
Methods
Clear Method


Raise Method