You are on page 1of 1

Actually, so what we have here is we have just a little main function.

We still have the same addtwo.


And we have, you know, this is just like we had before, I as
a regular int. But then J and K are const integers, and you can
see that we can add the const either on the left or on the right.
There are warring factions that argue that it belongs on one
side or the other. But the important...
The compiler is neutral. The compiler doesn't care.
Yeah, the compiler doesn't care. You can put it in either place.
And so we initialize each of these with this addtwo expression.
So all of that is fine.
So then this addtwo is also fine. Because we're making a copy
of the J as we said, because it's passed by value. We're going
to copy the J and we're not going to actually modify the original J.
But as soon as we try to assign to J or change its value in any
way, so even if we were to say J++, that would not work.
Similarly, for K, if we were to try to just, you know, assign
a new value, 7, that's not going to work. And if we build,
we'll see that the compiler is going to report error messages
and this time, it's actually a very easy to understand message.
It says, you cannot assign to a variable that is const. So this
is an extremely useful feature for
all code, use it everywhere.
Use it everywhere. When you declare a variable and you think
that you know it's going to change, because this is the value
I'm calculating, the total, the sales tax, whatever, then obviously
don't mark that const. But most of our programs are full of numbers
that are not going to change. And not just numbers either.
Variables of all types. And if you have the habit on every line
when you declare something of asking yourself whether it's going
to change or not, you will end up with more correct programs
that are easier to maintain in the future.

You might also like