In general, passing by value means that a copy of variable is made with the same value and passing by reference means the variable object itself is passed. You can change the value all you want with pass by value, but the original object is not changed. With pass by reference any changes to the passed variable also change the original. C does not support passing by reference, but you can fake it with pointers:
Faking call by reference is done by passing the address of an object and then accessing the value through indirection. That way you can get to the original object and make changes, but the address is still passed by value.
Last edited by Tom Gunn; Aug 24th, 2009 at 10:56 am.
What is the purpose of using call by value and call by reference?
Your question revealed that perhaps you are not understanding what it has been explained so far.
There's not such a thing as call by reference in The C language. Everything done is done by coping values around.
It is like a World where credit hasn't been invented. Anything you buy you need to pay with cash. The bigger the purchase, the more cash you have to carry around, the more cash you carry the more effort you have to make.
Pointers are like checks. Now you don't carry money around, but rather the small weight of a check book. And with a simple check you can provide the value of a much heavy and cumbersome pile of cash.
On the other hand. Call by reference in some other languages is like paying with Credit Cards.
There is no hood to look under. C does not have pass by reference, only pass by value. Pass by reference is faked in C by passing pointers by value and using indirection to get to the object.
Quote ...
A side note : one of the main reason call by reference was created is
because passing a class object by value is expensive.
You can think of Pass by reference like a pointer in disguise.
I think you are talking about C++, but even in C++, the main reason references are part of the language is to support operator overloading. They are also a convenient way to make output parameters, but that is a side effect of the original goal.
Last edited by Tom Gunn; Aug 25th, 2009 at 3:19 pm.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.