- [Instructor] Parameters are passed to a function
by declaring them inside the parentheses
of the function definition and passing them
in the parentheses of the function call.
Here's func.CVP from chapter five of the exercise files.
Let's pass an integer to this function.
I'll start by declaring the parameter
inside the parenthesis here in the function definition,
I'll say int I, so it's going to take an integer
and then I'll replace this puts with a print F
to print the value of I.
And now down here in main, I can pass a value,
I can say 42.
And when I build and run,
you notice that it says the value is 42.
So I've passed this literal integer.
I've passed it and it's I, once it gets into the function
and we print it out.
So by default, functions
and C plus plus pass variables by value.
So if instead of this literal, I declare a integral,
I'll call it I again and I'll give it this value here.
And then I pass I to the function.
And then if I come up here in the function
and I changed that value, and I say, I equals 73,
what do we expect to have happened?
Well, because it's passed by value,
we take this I here and we pass it the function
and it's a value is actually copied into a new variable,
a new integer called I and that's the I here
that's set to 73.
So this will print the value of 73.
But down here, if we want to print it again,
we say print F, the value is percent I.
And I'll just say the value after func.
And that should still be 47
because we're passing the 47 here, but this I is actually
an entirely different variable than this I.
So when I build and run, you see, it says the value is 73
inside the function.
The value after func is still 47.
Now, if you do want to actually change the source variable
and that's what's called a side effect.
If you want this to actually affect this,
that's called a side effect.
And you can accomplish this with a reference.
And all I have to do is come up here and change this I
to a reference.
And now, this I is the same variable as this I.
It's not making a copy anymore.
It's just saying, oh, okay, I'll use that same one
because we're passing as a reference now instead of a value.
And so when I build and run, you'll notice that the value
after func is now 73.
All I did was insert that reference operator there
and now it takes the reference rather than making a copy
of the variable.
So this is obviously pretty dangerous.
If I have this function, like off in library someplace
and it's actually changing the values in my main program
without telling me that it's doing that,
you can see where this could create bugs
and really have somebody pulling their hair out,
trying to figure out what's going on.
And so, this is why it's commonly considered good practice
to put the const identifier before this reference
in the function.
And this specifically disallows the changing
of that variable.
You notice I got this error here.
It says, cannot assign a variable I
with const qualified type const int reference.
So a whole other way to do this is with a pointer.
And this is common, especially when interfacing
with libraries that are written in C
and not C plus plus because regular C
doesn't have references.
And so, it accomplishes call by reference by using pointers,
which are actually more explicit than the reference.
You don't get the unintended side effects with a pointer.
So here, I would say, int pointer I.
And then I would de-reference this
with the de-reference operator.
And then, I have to actually pass the address
of the variable using.
Again, it's the reference operator, but in this context,
it's usually called the address of operator.
And so, now when I build and run,
we still have this side effect.
The value after function is 73,
but you notice that now I don't really have any question
of why, because I actually passed the address
of this variable.
So, it's more explicit.
And I have more reason to understand
and to expect side effects when I'm passing a pointer,
rather than just a variable.
But again, it's good practice and you can protect
this variable from side effects by using the const modifier
there and now we get this error,
read only variable is not assignable.
It's worth noting that internally, values are passed to
and from a function on a small data structure,
called a stack.
The stack is a relatively small space
and requires processing power to manage.
Passing large values to a function
requires copying larger amounts of data on the stack.
And this can be time-consuming.
It can cause a stack overflow,
which can crash your program
and create security vulnerabilities.
So when you pass something larger than a simple value,
you'll usually want to use a reference or a pointer.
So for example here, if I include the string library here,
the STL string library, we'll define a string object
here in main.
So that's an STL string, which is an object.
It is not a C string.
And so, that S does not represent a pointer,
it actually represents an object.
And so if I want to be able to pass a string object
to my function here, then I need to obviously,
change my parameter list.
And I'm going to say const standard string,
reference S because I don't want
to pass the whole string object on the stack.
That's considered bad practice and it can lead
to all kinds of problems.
And so instead, I'm passing a reference
and because I'm passing a reference,
I'm using the const modifier because that's the safe
and correct way to pass a reference.
And now here, I can just say the value is
and as dot C string and that'll return a C string
that I can print with print F and then down here,
I can pass S just like that.
And when I build and run, it works as we expected.
So, this is the accepted way to pass larger
than simple scalar values into a function,
using a reference and almost always,
it's going to be a constant qualified reference.
By default, C plus plus function parameters
are passed by value and on a stack.
You can pass pointers or references if you need
to pass larger values, but you need to do
so explicitly and safely.