Explain "passing by value", "passing by pointer" and "passing by reference"

Showing Answers 1 - 10 of 10 Answers

Sunita Karira

  • Jan 25th, 2006
 

A way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.

A way of passing the address[passing by pointer, reference] of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed.

SKarira

GAURAV MODY

  • Dec 30th, 2006
 

in "passing by value" the variables r assigned temporarily some values which the user provides it.i.e. this assignment is temporary

whereas in "passing by pointer" the values r assigned by changing the values at the location of the variables by using pointers .i.e. at thier address .this change of values for the variables is permanent

Whenever a function calls arguments they are passed in two ways...
call by value and call by reference


call by value
Copy of actual argument is passed thus any changes made to the formal argument [i.e. copy of actual argument] is not reflected in actual argument.


call by reference
Here address of argument is passed rather than the value. the compiler itself finds what value is stored on the address. any changes made to the formal argument is reflected in actual argument also b coz the value is modified at the address.


We prefer call by reference over call by value.
coz with later their are more chances of making mistake.

  Was this answer useful?  Yes

Pass By value & Pass By Pointer are the terms came from C.

Pass By Value is used when the called function needs the value for computation, and the computed result is returned back to calling function. calling function does not see any changes made on passed args. called function does not have ownership of the passed args.

Pass By Pointer is the case, when calling and called functions both need ownership of the args passed. Little risky, if not handled properly, so most of the times, when no other option is left (to return multiple variables), this way of passing args is used.

With the need of Function overloading, function chaining, and to avoid risk with usage of  Pointers, the concept of Pass By Ref came with C++.
It is mostly useful in function overloading and function chaining, where one OBJECT is assigned to 2nd, and 2nd is assigned to 3rd.
When an Arg is Passed By Ref to a fn, it is mostly passed as CONST, to avoid accidental change of original object.
(We can argue that then we can use PASS BY VALUE method too, but then in that case local variable is created, and constructor, destructor etc are also come in picture).
REF is also used while returning the REF of the calling object itself, to enable function chaining.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions