- Now that we discuss how data
gets passed on to a function let's look
at how values or results are being returned from it.
We have several options.
Option One is very explicitly.
We tell the function to return the value.
If any of that function does not recieve
any arguments from the outside.
In fact this function doesn't even have
parameters to receive anything.
So it simply returns a value.
Very simple.
Option Two is modifying the value for global variable.
For example, we would have a temperature
variable defined in the global section.
That variable is then modified inside the function,
and since there are no other local variables
with the same name it is fair to
assume that this is the same variable temperature
that lives outside the function.
Finally the Third Option is
passing arguments by reference.
We have for example, in this case,
two integers a and b that are going
to be mapped onto two pointers
that represent the function parameters.
Those pointers then modify i and j.
They modify the address in which
those two outside numbers exist,
and by calling func with the &a and &b
we are passing that address.
So we not really modifying a variable
but re-modifying the address.
Remember that the variable is just
a label of an address.
The pointer gives us the mechanism
in the language C to access that address.
Let's see how this actually works.
So here's our global variable temperature,
and here are three functions.
A func, named func, that simply returns a value, 12.
The second one it's a function measure
that modifies a global variable,
and the third is the function
that receives two values passed by reference.
Please recall that our topic
of discussion is Functions That Return Values,
which means passing parameters
by reference are not part of this discussion.
We're only talking about functions
that are doing something that affects
a variable outside their scope.
So let's look into main
and see what this program does.
Actually it's even better if we run it
so we can follow step by step.
So first I am declaring a variable n,
which receives the return value
from a function.
So look at the function definition.
This is simply an end value inside
a function that's being returned.
So the function communicates to
the outside world explicitly
the value of an internal variable.
So it's not surprising that after
the call to func n = 12.
Next...
Measure is, as we saw, affecting a global variable.
If you look at the definition of measure
indeed it doesn't declare anything internally,
and we already know that temperature
has been declared as a global variable.
So there is no surprise that temperature
is now 50.2 as is the value set
to it inside a function.
Finally, Line 30.
I am declaring two variables a and b,
which, as you can see, are deliberately not initialized.
I am now calling the func by ref function
with the addresses of those variables.
That's very important.
I not sending to the function a and b,
but the variables that represent
the address of a and b.
Let's hover over the func
and see what it does.
There are two pointers which also happen
to be the two parameters of the functions.
They are the ones that are being modified.
So the function has received two addresses a and b.
Inside the functions those addresses
are being pointed at by two pointers i and j.
So when i becomes 5 and j becomes 7
this means that the address that
this pointer i and j is pointing to
has received the values 5 and 7 respectively.
See this address is the same as the address
labeled outside the function as a and b.
It follows that a and b have now been
modified outside the function.
You can use this approach to create
a function that initializes data.
In fact this is what this function did.
We can create a set of variables that
are not initialized anywhere
and then call a function by reference.
That function can be even stored
in an external library that's being
loaded and in a very safe way we are
very explicitly initialize every single
variable that require initialization
in our code.