Hey all,
Can anyone please explain what is the difference between a pass by reference parameter and a pass by value parameter and why a parameter is important at all?
Thank you in advance
Hey all,
Can anyone please explain what is the difference between a pass by reference parameter and a pass by value parameter and why a parameter is important at all?
Thank you in advance
Consider this post signed
pass by reference is when the parameter references the original value passed into the method (by using the memory location's address)
pass by value is when a copy of the value is given to the method by parameter instead of a reference to the original. Any modifications done to this parameter in the method don't affect the original value because it becomes just like a local variable.
You may also want to look up pass by value-reference, and pass by name.
Last edited by TieFighter; 03-31-2010 at 04:21 PM.
Not an array. Pass-by-reference (which is a really bad name) essentially means passing the address of a variable to a function instead of its value. Thus, knowing its memory location, we can modify that memory location and hence the original variable.
Since all parameters are passed by value (copies are made), any value you pass will be duplicated into a new variable in the new function, hence any changes you make to that variable will not be reflected in the original variable you passed.
My bad - I meant method instead of array. I also don't think you meant to imply all parameters are passed by value.
TieFighter, I hope you didn't mean "method" anywhere. That's some OO abomination we don't speak here.
YES YOU CAN!
However, TieFighter, in an anal retentive technical sense (the sense in which say, an exam question might occur): NO YOU CAN'T! When you pass &a, you are still passing a value -- the address of a.
It is somewhat silly, perhaps to disambiguate C++ references from C pointers, etc. I am sure there are more historical reasons too. Silly, since in any case all languages that permit "pass by reference", somewhere under the hood, are passing by value. So in truth you cannot "pass by reference" on a computer, computer memory is not an abstract realm.
Point being, I'll second TieFighter and say it is not so unusual to refer to passing by reference in C, but also second Elysia in saying it is not so unusual to hear people observe that this is not truly passing by reference, since the mechanism is transparent (references are opaque).
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Okay... you just defined how pass by reference is implemented. If any professor calls that pass by value, then I'd ask for some of my class money back.
If a professor calls passing a pointer “pass by value” it simply shows he's familiar with C.
There is a distinct difference between languages that have actual pass by reference (such as C++) and those that don't (such as C). In C, you have to “do” something with the parameter before you get to the original object. This is as simple as dereferencing, but that is still an action; in C++ no such special action is needed. When you pass by reference, you get the original object automatically. Sure, under the hood a pointer is passed around, but from the user's point of view, they're different. This is the point of saying that C is only pass by value.
Note: I'm not arguing that calling the C approach pass by reference is incorrect. What I'm saying is that it's not wrong to call the C approach pass by value. You can say “I'm passing a value, and that value is a reference to some other object.” It's an annoyance of terminology; but calling C a strictly pass-by-value language is not incorrect.
I don't think it'd be an issue at all if all languages had either “true” pass by reference, or none did (necessitating dereferencing)—but there you are.
Oh dear. Either you're confused with C++ or you complicated that unnecessarily.
In C++, there is a type T&, but in C, there is only T*.
Actually, here's a thing:
The C++ standard says that references are aliases and that they do not need to take up any memory. So technically, a reference does not have to be a pointer, although that is exactly how they're implemented in many popular compilers.