So why not just do everything in Maine?
Why even have a function?
So to answer this question, we're going to look at a previous problem.
This is something that I've kind of smashed together, but you must remember this.
It was kind of the last thing we went over with a for loop.
We had this problem that we solved where we calculated a whole number raised to a power right and explain.
So this is all good, right?
We did this program, it seemed to do what we wanted it to.
It doesn't seem that bad to have this in Maine.
It's not too big, right?
But what have we changed things?
So would I add some more stuff to it?
So this is what we did before.
Right.
This what the program had before, but now we say our program wants to do it over again with some hardcoded
values.
You notice when we make another integer here called three.
What do we put in it?
Well, we end up using the result from this previous calculation and we add it to the value three.
So that result gets saved in i3.
We also then make an AI for which will be like another exponent, so this i3 will kind of be like a
new hole.
No i4 will be like a new exponent.
And we kind of do a similar thing, but what we do is three plus whatever I two currently has at this
point in time, which will be whatever the user entered up here for it.
We're still doing a procedural program where we kind of go top to bottom like this.
So I too already has this value that came from the user result has whatever was calculated here that
we printed out, and we add that to the value three.
So add three plus result.
Put it in here and take three plus I to put that answer in here.
Now we have a new hole number and a new exponent.
We do the same thing with the result by resetting it to one right result has been changed.
You know, by doing all this math and here we want to reset it to one just like Line 13 so we can start
this process all over again.
So we do that and then we start the process all over again, except rather than doing eyes less than
I do.
Also want to point out that, you know, my naming was a little bad.
These are integers that the user entered into.
I was for iteration, right?
I could have used another variable.
I kind of told that to you last time.
You don't have to use.
We call it whatever.
But just so you know that there's a separation in your mind.
It's not isFor iteration, the eye one eye to eye three or four.
Those are the integers that we're considering for the power thing.
So we do it again, right?
But with our new values.
We're saying that while go as long as I've listened, I for restart is zero to eight plus plus do resort
times equals three to get a new power right?
What if we just kept doing this over and over again, though, what if I did the same thing again like
this, this whole chunk, just like we repeated it here?
What if I repeated it again?
And then what if I repeated it again and again and again and again?
It's kind of annoying, actually really annoying that I have to keep rewriting this for loop, right?
I got to rewrite all this like.
Code here, and it's just getting like a little irritating, so.
What if we instead make a new function that does this calculation for us?
It actually does the work to figure out the answer of raising one number to another.
No power.
It does the Ecsponent calculation for us.
It does this for a loop.
All this code is in there and then we can do it as many times as we want.
We can call it from wherever we want in our program.
But it will handle the work for us.
We don't have to keep rewriting stuff, we can just rely on the function to do its job right.
We'll just say, Hey, function, your job is to calculate a power like an exponent.
I'm going to give you one hole number and one exponent and you're going to do the work and you're going
to give me back the answer and then I can print that answer out to the user.
So let's go ahead and do that.
All right.
I am actually going to go here.
I have this in the Visual Studio ED.
And what I'm going to do, I think I left my.
Yep, I have my console down here still.
So what I'm going to do is I'm going to make a function for this.
So what I will do is I will say and and sorry, it's still a little hard for me to type a slight wrist
injury, so it's going to be a little slow and I'm just going to call this
power.
Or I'll just say, what about Kalik power or something like that?
I'm going to need to give it a whole number parameter, right?
We're going to pass it some data, right?
We're going to like send it up some data for to do the calculation with two things actually right.
We need a whole number and we need an exponent.
So I'm actually going to say type integer, right, because it's going to be an integer.
We noticed that we used an integer for the whole number and an integer for the exponent.
So I'm going to call this and whole and and I'm just going to say exp instead of exponent.
So then I put some curly braces press enter, make some room here.
All I'm going to do is copy paste this right and I'm going to change some things.
So what I will do is I will copy paste this when I control.
See?
I'm going to go up here, and I'm sorry.
So Resort is not known because it hasn't been defined here.
I'm going to go ahead and delete it, and instead I'm going to define it in this function.
I'm going to say end result equals one now.
The function knows what result is before we defined our function in Maine and we didn't pass it.
To the function, right, the function doesn't know about any data unless you pass it to it, right?
It can't do anything with that result unless it makes its own data.
Here, we're clearly defining a new variable inside the body of this function.
This is an important concept.
It's something called scope, just like inside of the air for inside of the for.
If you're inside the body of some type of function or control control structure, you must make sure
that the things that you're using.
Are in the correct scope for you to be able to use them, right?
Certain things inside curly braces may not be known by stuff that's outside of the curly basis.
Here we're making sure that we have made a new function, a new variable inside of the curly braces
of our function.
So it's in the body that way.
The function knows about it.
We're not going to care about it being in Maine anymore because calc power does the work now.
Maine doesn't need to calculate aerosol, right?
Maine is just going to receive the answer passed back to it.
All Maine is going to do is get the input data and then it's going to send it to scout power, how power
will do the result and pass the answer back.
Of course, so now we say Entwistle equals one.
But what we're going to do is actually use our data.
And these variables, rather than use, I want an eye to.
I won.
Was the whole right?
Too is the exponent, so what we're going to do is just rename these right because we have a hole right
here.
I want is the whole number.
So instead we'll call this hole.
I, too, is the expert on it, so we will call this XP because that's what's right here.
Mane knows about the hole in the exponent, but only it knows it in the context of i1 and I, right?
So what we can do now is we can delete this because our calc power function is doing this work now.
Let's let it do that work.
So now we're go we're actually doing the calculation here.
What we will do then is pass back the answer right in the results.
So what we will say is return result.
So after resort has the proper answer, which is once the loop is done running, think back to your
for loop program, right?
Result kept doing multiplication over and over again until it had the correct answer.
Now that it has the correct answer on online 10, we can just return it and that is what's getting passed
back to our call.
We don't have a call yet, though, so let's go ahead and make that call.
So what we will say is we will say and answer equals calc power.
I see that right here in my editor, so I'm going to go ahead and press tab.
Then I need to put the parentheses and here come the arguments, right?
Let me see if you can guess what the arguments are going to be.
What am I going to put in here?
Separated by a comma?
So take a moment to think or pause the video.
What I'm going to do is I want and I to write because main reads the data from the user input into I
one and does the same thing, reads the exponent into it too.
So let's do that.
We're going to put AI one and then a comma, and I'm going to put a space you don't have to, but a
space if you don't want.
But I'm going to say I too.
And this is what gets passed up there.
This whole is the whole no, and it gets passed up to this position here, where it says whole I to
holds the exponent it gives passed up here to the exponent.
So now we have our answer here, right?
So once it does the calculation, it passes the answer back to here.
And then we save that value with the assignment operator into this new variable called answer.
Instead of doing result here, we will print out answer.
Then down here, we can replace all this stuff to.
So instead of I want an interview in Maine, we're going to use an eye 394 as our arguments.
But what we can do is delete this stuff just like we deleted it up here.
So I'm deleting that because calc power.
Does that work now?
Press delete.
What we're going to do is we can reuse this, but we're going to get a new value in it, so I'm going
to say answer is now equal to count power and I'm not going to put I want an to this time, I want to
send it.
I three and I four, so I say a three comma I four I three is the whole no, I four is the exponent,
so I three gets passed up to here.
I four gets passed up to here, and we're doing we're having it do this same task, but with different
data now, right?
And we need to find our answer variable up here.
We don't need to make another answer variable.
We can just reuse it right here.
What's happening is that when we get our new answer from the power, it gets overwritten into here.
So answer whatever is an answer, which is whatever came from here, right?
That's the last thing we did.
Top to bottom the last time we did something with answers when this got saved into answer here what
we are doing is kind of taking.
A new value from Cat Power overriding what was in answer and putting the new value in there.
So it's kind of like a race or whatever is in answer.
Clear it out.
Put this new thing in answer.
Now what we can do is have the same kind of see outline, but instead of I want an I to, we have three
and four and we still put an answer here.
So we print out the answer.
We do it all over again with these new values.
But it looks a lot cleaner, right?
We just kind of outsourced our work to this function and we said, Hey, how power handle some stuff
for us.
Can you do this task?
I want you to be able to calculate the power and then just give it back to me.
Then I can print it out to the user in the console.
And it says, sure, let me give you that answer and say, Oh, thanks for the answer.
That's what Maine says.
So here's some data I'm going to pass you some data calc powers like, Oh, cool, let me take this
data.
Let me do some calculations.
Let me pass you back a single integer as an answer.
Maine says, Hey, I got it right here.
This is where I caught the data.
You just gave me the answer.
Let me save that in a new variable that I made called the answer on this line.
It says, OK, I'm ready to print out some stuff to the user and then print out that answer I got in
Maine says, Hey, you know what?
I'm going to do it again, so let me make some new hole and exponent values on my own.
Rather than getting it from the user, then it says, Hey, cat power, can you do some work for me
again?
Let me pass you some more data and cow power says, OK, great, pass me the data.
I'm going to do the calculation.
I'm going to pass you back.
Another answer and Maine says, Hey, thanks, I got your answer right here in the highlighted section.
I saved that answer into answer again.
What I did was I overrode it, and I put it in answer.
Now I can print out this new value that you passed me, and I do that with the C outline.
I say I three and I four this time and I print out the new answer I just received from Cat Power.
Hopefully, that was enough of an explanation for you to understand that I'm going to save this, so
I went ahead and saved it.
Right now, I actually you can do this to you and say right here.
Let's go ahead and recompile this, I have a command right here to compile it.
And this is actually function practice to.
I just pressed up on that.
Let me do this f l practice uh to.
Our source told us that it helped to see.
Oh, yeah.
We have this problem.
So on line 24, I have a result here, but it's no longer called result.
It's called answer, so I have to change that.
That was an error.
So the thing that it complained about here was I had the weird result here.
And so it said OK on line 24 starting and character 18.
It said result was not declared in this scope.
Remember, we talked about scope.
So inside the scope of main, which is inside the curly braces.
The compiler said, Hey, you're trying to add result to three, yet I've never seen the word result
before, and that's because since we're doing top to bottom.
There was no result, right?
How can C++ know what the variable result is when we haven't even made it?
What I did was I changed it to answer.
And so when it said result right here in this zone, it was saying, OK, I don't know what that is.
I can't compile this because you haven't told me what the word result means.
You haven't told me what the variable holds or anything.
You haven't defined it.
I actually meant to put answers.
So now I changed it to answer.
Let's go ahead and recompile it.
In fact, what I have to do first is save it.
So let me save it less.
Recompile it now.
It's OK, I'm going to have to clear.
And now what we can do is we can run this.
So f l to um, was it no fail?
What did I call that NFL PR too?
So either way, I felt help are to run that.
Let's get some numbers.
I'm going to just say two to the third power.
And then what it's going to do is it should print.
This first two to the third power than what it's going to do is it's going, let's just predict this,
right?
That's what I'm would do.
Let's see if we can predict what the answer is going to be.
So we have to turn the third power.
So what it will do is call cow power first with two and three.
It should return the value eight, and it should say.
To it, this is the carat symbol, two carat three is equal to eight.
It'll do an inline go to a new line.
It'll then say, I'm going to make a variable, I'm going to do three plus eight because eight was the
answer, right?
So it's eight, nine, 10, 11.
It'll say, All right now, I three has 11.
Then I'll say my four is three plus eye to eye.
Two is going to be this value three.
So three plus three is six.
It's now going to calculate 11 to the six power.
Honestly, I don't know what that is.
Off the top of my head, but that's what it will print out.
It'll say 11 to the sixth power is going to be whatever that answer is.
So I when I press enter, that's what we really should see.
And there we go.
It's a big number.
Hopefully, that is correct.
You can double check that with a calculator.
In fact, I think we could do that here.
So I will just say 11 x to the Y six equals, and that looks like the same number right there, right?
So one, seven seven one.
Five, six, one, so that looks good, good to me.
So that is pretty cool, right?
We went ahead and outsourced all that work to our calc power function, so you can probably imagine
just how much you could do now by having functions, how nice your program could be.
Imagine having many, many functions that do many things.
We're about to be able to write very large programs that do a lot of complex stuff, and these functions
are going to help us a ton.
This is probably a lot for you to absorb.
I'm going to have more function videos, and in those function videos, we're going to look more into
depth about what's going on with functions.
And in fact, we are going to use more functions, not just one.
So you can get more practice.
And once I explained some more things using multiple functions, we will go over some problems just
like we did with loops, so you will get a chance to exercise your newly learned skills.
All right.
So with that, I think that's enough that I've stuffed into your heads in this lecture about functions.
Rewatch the video if necessary, I will be doing more function stuff.
Don't worry if you're confused or nervous or feel like this is too much.
We will practice so you can better understand and absorb it.
With that, I will see you in the next lectures.