(upbeat music)
- Sometimes it's necessary
to pass values to a function.
Let me show you what I mean.
We're going to create a new program
and I'll call it PassValues and I'll click OK.
(mouse clicking)
All right, in this program I'm going to start
by doing the using namespace at the top.
(keyboard clanking)
Let's create a function that will calculate
the average of three numbers.
So it's going to return the average, so I'll call it
double average, and it's going to take in
three double values, double a, double b and double c.
Okay?
And the function will return the value of adding
a plus b plus c.
So I'm just going to say return (a+b+c) sorry,
and I want to divide that by three
so that it will calculate the average of any three numbers
that the main function passes to it.
All right, in the main function,
let's create three more variables.
Let's create double x and set x equals to five
and then we'll have y, y can be equal to 10
and then we'll have z and set z equal to 15.
All right, so we want to calculate the average
of those three numbers and then print it out.
So let's create double avg equals a call
to the average function, and we're going to give
the average function x, y, and z.
Now, what's going to happen is the values five, 10 and 15
will be substituted in for x, y, and z.
Then we get to line five,
a will have the value of x which will be five,
b will have the value of y which will be 10,
c will have the value of z which will be 15.
And when you add those together and divide by three
you get the value 10.
So let's print it out.
(keyboard clanking)
Now let's run the program.
(keyboard clanking)
And we get the average is 10.
So it's not real hard to understand the process
of sending values from one function to another.
Remember, the list of values inside the function
is called a formal parameters.
In this case, we have double a double b and double c.
Now, what I want to show you also is that I can change
a, b and c inside the method average
and it will not change x, y, and z in the main function.
So let's just say we're going to do a plus equals three,
and let's do b plus equals four,
and let's do c plus equals five.
Now this is going to change the value that gets returned
from the average but it's not going to change
the values of x, y, and z.
So down here, I'm going to just print out the values
of x, y, and z, so we can see them.
So I'll say x,
(keyboard clanking)
and I'll do the same thing for y and z.
So I'm going to copy and paste this two more times
(keyboard clanking)
and let's make this one y
(keyboard clanking)
and this one z.
(keyboard clanking)
It would also be nice to be able to see
the values of a, b and c as well.
So let's copy these three cout statements
and let's paste them up here,
and let's change the values to be a, b and c.
(keyboard clanking)
Now let's run the program and see what we get.
This time we see a is eight, b is 14 and c is 20
which is because in the function we actually added values.
So the average of those three is 14.
But look at x, y, and z,
they're still five, 10 and 15 respectively.
And again, that's because x, y and z
are declared locally inside the main and a, b and c
are declared locally inside the function, all right?
Now, the next thing I want to do is I want to actually
show you how we can pass these values
instead of pass by value.
In pass by value, it makes a copy of the value
and then a has its own value going forward.
But there is a way to actually say, pass by reference.
What we can do is we can change these variables up here
so I can change a, and I can say double ampersand
and b I can say double ampersand.
Remember the ampersand is the address of variable.
So what's going to happen is it's actually going to have
the address of x, y, and z now in our a, b and c variables.
Let me just add an endl here so it's a little easier to read
(keyboard clanking)
and I'll do the same thing down here.
(keyboard clanking)
All right, this time what's going to happen
is the three values, five, 10 and 15,
are going to be passed a, b and c.
But when I change a, b and c in my function called average
it's actually changing the value that's pointed to
by the address of a which is the same address of x.
So you'll actually see x, y and z will be changed
when we're done.
So let's run it again.
This time when I ran it, a is still eight,
b is still 14 and c is 20, and the average is still 14,
but look what x, y, and z this time.
When I do pass by reference, it actually press a pointer
to where that value is in memory.
So when I change it in the function from five to eight
and from 10 to 14, and from 15 to 20, it changes it
in the main method as well or the main function as well.
So when we're passing values to a function, we can either
pass by value which means you make a copy of it
or pass by reference.
When I pass by reference, that means that I'm passing
the address of the variable in this case, the value five,
10, and 15 respectively, to the average function
which is changing those values.
So when I come back from the calling function
back from main, those values are changed as well.
(upbeat music)