- Continuing our discussion of functions
is the topic of passing variables.
Parameters of a function are essentially
the same as local variables but are assigned values
outside the function and their value
is carried inside the function.
There are two ways to pass variables to a function.
One, it's called by value.
Argument values are copied onto the function parameters.
So, the variables themselves do not participate
into any computation or any statement outside the functions.
They are just receivers of values from the outside.
By reference, the argument addresses
are copied into the function parameters.
So, the distinction here is that we are not passing a value
but we are passing the address of
where the value is being stored.
That's a very, very important distinction
because now the function is capable of modifying an address.
So, there is no longer a distinction
between local or global.
It is the physical location.
Let's see how this works in code.
I wrote two functions.
First of all, let's look at their declaration.
Notice line 15, void someFunction
receives two integers: a's and b's.
On line 16, the function someOtherFunction
also received two parameters but they are pointers,
meaning they are linked to a specific
address that stores an int.
What I will do now is I will initialize x and y to zero.
I'm going to call the function
that receives the parameters by value.
I will check the values of x and y before
and after the call of the function
and then I will repeat those steps by reference.
This way we'll be able to see what
is the difference between passing a variable
to a function by value or by reference.
I'm going to run the code and we will look
at the output line by line to see where it's coming from.
First, notice in line 25 x = 0, y = 0,
and indeed before the function call, they are zero.
Now, we are calling someFunction inside of function.
The variables have been renamed a and b.
It's not that the x and y have changed
but a and b are the parameters of the function.
The function internally knows them as a's and b's
so the values of zero have been copied onto a and b.
That's why on line 50 we're printing a, 0, and b, 0.
Inside the function, we are now modifying
a's and b to be 111 and 222 respectively.
Hence the note modified inside the function,
here are the values.
The function now ends and the execution returns
to the line after the function call.
And after the function call x and y are still zero
because we passed values onto the function.
Those values were used inside the function
for whatever calculation the function needed
but they had absolutely no effect
on the x and y outside the function.
Let's see what happens when we do this by reference
when the argument that's transferred to the function,
it's actually the address of a variable and of the value.
Before the function call, they're both zero,
and inside the function something different will happen.
First of all, I'm calling this function
with the address of x and y because
this is the way we treat two parameters
that are defined if recall as pointers.
And if you not, you can simply hover and they will tell you.
Eclipse is very good at giving
you information about your code.
You can hover over a function and see
it's entire definition without needing
to scroll up and down.
So, inside that function, we have two pointers,
a and b, that are being assigned a value, 111 and 222,
which means those pointers now have a link
to the address where those numbers are being stored.
Therefore, as you can see, in the function
a was modified and x and y were modified
because those pointers that happened
to be parameters of a function
they point to the same exact address
as the variable x and y.
We'll refer to them by different labels
but they are the same exact address.
This is why the address has been modified
inside the function and the change is persistent.
So, whatever label we choose for that address,
it is still the same address outside
the function and inside the function.