the parameter "x" of the function corresponds to a memory area local to the function.
This means that, when we invoke the function by passing it
as an argument, a variable which is defined elsewhere
in the program, for example here, in the main program.
At the moment we call this function, we will copy the value of the variable
passed as an argument into a memory area local to the function.
This means that when I execute this code, I am going to modify
the local area, in which is stored the value of the variable we passed as an argument.
This is passing by value.
To indicate that we want to use passing by reference,
we have to declare the function header in a specific way, in C++.
We will indicate, using a specific symbol, the "&" (ampersand), that the function "f"
should use references. When we define the header
of the function in this way, we indicate that, when the function is called,
the parameter "x" for this function is a reference to the variable that
we passed as an argument. It is just another name for the variable "val" passed
as argument. Here, the variable "val" was equal to 1, and the name "x"
references the same memory area as the one occupied by "val".
So in this case, incrementing x will also increment val.
When we pass an argument by value, the function will work
on a copy of the argument, which means that any modification that we make
within the function does not affect anything outside the function.
We are working on a local area, and the argument is not modified.
When we use passing by reference, we indicate that the local variable
which is passed as an argument corresponds to a reference on the object
associated to the argument, when the function is called. So, the main difference
is that any modification made within the function also affects
the outside, and is visible when the function ends.
In C++, we indicate that a function uses, for one of its parameters,
passing by reference by using a particular symbol, an ampersand (&).
When we prototype the function, we can use it to indicate that
the parameter x is passed by reference. In this case, it is a double
which is passed by reference.
To summarize, when we pass by value, the parameter of the function
is a memory area local to the function and any modification affects only the
local area, and does not affect the variable which was passed as an argument.
When we pass by reference, we indicate that the parameter for the function
is simply an alias, another name for the variable that was passed as argument
and that any modification of the parameter will also modify the variable
passed as argument. Now, if we return to our little introductory example,
we had a small main program which declared a variable "val" containing
an integer of value 1. Then, there was a function call.
If we examine the header of the function, there is no particular symbol,
no ampersand, which means that we are passing by value.
Since we are using passing by value, this means that x, the parameter
for the function, is a local memory area and thus that the value of "val"
is copied into "x".
Modifications to "x" will affect only "x". If we display "x", we can clearly see
that x was modified. However, when we are done executing the function
and we display the value of "val", we can clearly see that its original value
is unchanged, and that "val" is still equal to 1.
If we take this same example, but indicate that the function uses passing by reference
with the ampersand symbol, we still have a variable local to
the <i>main</i> which contains 1. When we make this function call, since there
is a passing by reference, we simply indicate that "x" is another name for
the argument passed to the function, "val". This means that here, whenever
I modify "x", I also modify "val". Displaying "x" clearly shows that x
was modified. Displaying "val" also shows that "val" was modified.