0

I'm a Python programmer learning C++. In Python, parameters are passed to functions as copies (assuming that parameters are non-mutable). This means that the original values of parameters are not local to stuff happening inside the function and therefore can't be changed.

In this following C++ snippet, I see that this function takes in a reference of s, which, if edited inside the function, can change the original value of s. I don't understand why this is the convention, aka. why &s is used instead of s.

void write_comment(const std::string &s) {
  std::cout << s << std::endl;
}
Adam Wilson
  • 140
  • 5
  • 1
    Because "s" is already reserved for pass-by-value. Conventions are just that, frequently there's no deeper meaning behind them. – PlinyTheElder Jan 10 '20 at 22:14
  • Note that the `const` keyword used in the example ensures that the argument *cannot* be edited inside the function (at least, not without resorting to 'clever tricks' that invoke undefined behavior) – Jeremy Friesner Jan 10 '20 at 22:15
  • The idea behind a const reference is that in fact, only the size of a pointer is copied to the function scope instead of the object (std::string), which can have a higher size than a pointer – Adrien Givry Jan 10 '20 at 22:16
  • @JeremyFriesner Woh, that makes sense. Does this mean that pass-by-ref and const work together to ensure both speed and immutability? – Adam Wilson Jan 10 '20 at 22:16
  • 2
    Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). All of this would explained therein. – NathanOliver Jan 10 '20 at 22:17
  • "In Python, parameters are passed to functions as copies (assuming that parameters are non-mutable)." **absolutely incorrect**. Python objects **are never copied** when passed as arguments. Python uses *neither* call by value *nor* call by reference. – juanpa.arrivillaga Jan 10 '20 at 22:52
  • @juanpa.arrivillaga It uses pass by *object reference* which is another way of saying "pointers to objects passed by value". This distinction is often hard to explain. – tadman Jan 11 '20 at 02:19
  • @tadman the distinction is real, and it isn't really that hard to explain. The problem is that in many people's minds there are only 2 evaluation strategies: call by value / reference. So then, when python's evaluation strategy gets explained, they get told things like "pointers to objects are passed by value" even though *python doesn't have pointers*. This explanation confuses the semantics with the implementation. Here's a thought experiment: you can implement Python in Fortran, using only call-by-reference. That doesn't change the evaluation strategy. It's still call by object sharing. – juanpa.arrivillaga Jan 11 '20 at 02:27
  • @tadman and sorry if I come off as ranting, but there's always a deluge of questions that come up on the Python tag because of subtle misunderstandings like "In Python, parameters are passed to functions as copies (assuming that parameters are non-mutable)." I think it is due to people trying to shoe-horn in concepts from previous teaching languages into Python. I think people should learn the language on it's own terms. I'll admit, "pointers to objects are passed by value" is at least correct, unlike certain doozies I hear, like mutable objects are passed by reference, immutable by value! – juanpa.arrivillaga Jan 11 '20 at 02:32

0 Answers0