In the last lesson, digitCount divided its n by 10 until nothing was left, and
the number in main didn't change. That is no accident. This lesson is about
how values get into a function — as a copy or as the original — and about
where each variable lives and for how long.
Passing by value: the function gets a copy
An ordinary parameter is a new variable that gets a copy of the argument. Whatever the function does to it stays inside the function:
This is called passing by value, and most of the time it is exactly what you want: the function can use its parameter as a scratch variable, and the caller's variables are safe from it.
Passing by reference: &
Sometimes changing the caller's variable is the whole point. Put & after the
type, and the parameter becomes a reference: not a new variable, but
another name for the argument itself.
The classic example is swapping two variables. Here are both versions side by
side; step through and watch a and b: