Note that functions that change their arguments, when arguments are passed by value, should be rare. |
It’s hard to guess what that user meant when s/he wrote that sentence, but perhaps it can make some sense.
The topic “passing parameters to functions” (together with returning from functions) involves pass by value, which could imply copy elision, copy and move constructors, references, pointers, dynamically allocated objects and... I can’t imagine how far we could go.
IMHO, the problem is, if we stick to primitive types (int, double, char...) everything goes fine and we could simply ignore that sentence. Those types are so ‘light’ and easy to manage for the compiler that we can chose what we like better:
Do I want my value not to be changed by the function? I just pass it by value: I’m guaranteed the original value will remain unaffected.
Do I want the compiler to help me or my code to be easier to read? I can add a ‘const’.
As that poster said, in a situation like the following
1 2 3 4 5
|
int myfunc(const int i) {
int a = 13;
if(a = i) { /* ... */ }
return a;
}
|
the compiler will refuse to compile instead of just warning me - so, if I’m one of those who disregard warnings, I’ll be saved by the fact i is const.
Alternatively, do I want my value to be different after the function execution?
I can pass it by (non const) reference or I can still pass it by value and then remember to store the returning value somewhere in the caller.
So far, so simple.
But the more we write code, the less we find ourselves dealing only with primitive types. Actually, we usually need to deal with potentially expensive to copy types - and that means we very often pass our parameters by reference.
In that case, we usually say: if I want my object to be modified, I pass it by reference; if I don’t want it to be modified, I pass it by const reference.
Again, it looks pretty simple.
But what if I want my object to be modified, but later I want those modifications to disappear at the end of the function?
Well, it could sound a bit strange, but it can happen, doesn’t it?
If so, the question is: how many times does it happens? Is it a common experience for you?
We can obtain that easily passing an object by value and than not returning its new value, but the question is not how we can do that, but how many times it happens we want that.
I’ve tried to remember the code I’ve written recently, and it only came to my mind once I wrote a function which interpreted a code inside a string. Throughout the function I deleted the already interpreted character, modifying the passed string. The simplest choice was of course passing the string by value, so that it could be freely modified.
Now, either my memory betrays me, or I’m a terrible programmer, or it’s not common to modify parameters passed by value. It doesn’t mean it’s wrong, just it’s uncommon. Usually, if you modify a parameter, it’s because you need that new value later. But, if it’s passed by value, or you correctly return and store it or that modifications will die together with the function.
My interpretation is that user wanted to say: if you pass your parameter (which is not a primitive type) by value and you modify it, check your code again because it’s not so common to do that. But of course it could be the perfect solution for that particular case.
Or maybe I’m just writing a lot of <censored>. So, please, feel free to correct me as harshly as you wish, I’m eager to learn.