Friday 27 March 2015

Passing Objects as arguments to Function

It is possible to have functions which accept objects of a class as arguments, just as there are functions which accept other variables as arguments. Like any other data type, an object can be passed as an argument to a function by the following ways:

? Pass-by-value (a copy of entire object is passed to the function)
? Pass-by-reference (only the address of the object is passed implicitly to
  the function)
? Pass-by-pointer (the address of the object is passed explicitly to the
  function)

In the case of pass-by-value a copy of the object is passed to the function and any modification made to the object inside the function is not reflected in the object used to call the function. Whereas, in pass -by-reference or pointer, an address of the object is passed to the function and any changes made to the object inside the function is reflected in the actual object. The parameter passing by reference or pointer is more
efficient since, only the address of the object is passed and not a copy of the entire object.

No comments:

Post a Comment