Discussion 2-16

Today we practiced more with functions, and especially with passing arguments by reference (i.e. using &). The examples we coded are in byReference.cpp.

The main point to remember about passing by value vs by reference is: passing by value makes a copy of the input, which is then used within the function like it's a whole new variable. Passing by reference doesn't make a copy, and instead lets the function work directly with the data that's passed in. This has two main implications for our code:

Note if you want to pass something by reference for speed purposes but the function won't actually modify that object, you should pass it by "const reference" instead. This makes it easier to understand what your code is doing and also easier to debug; failing to do this when possible definitely counts as poor style and will cost you points.