Hello, and welcome to another video. In this video, we're going to talk about the concepts of functions. We're going to write a function, a simple function, and then we're going to run the function. So functions are just like what you're looking at here. This is a function. It's into your main function. Every C++ program has a main function. From the main function, you can call other functions. In order to create a function, you follow a few steps, just like this function here, we are going to create another function. I'm going to create a function called add that's going to take an add two integers together. And so I'm going to use the same data type as the main function. I'm going to start out with the data type. And what I've done here is I've closed, I haven't really closed it that it would be closing it, but I've reduced down main, put it down here just to get it out of the way. And I'm going to write another function above main so that I can call the function from within main. And the function I'm going to call is going to be called add. And add is going to add two integers together. And so the function kind of the prototype sort of looks like this. Where I've got integer add, add is the name of the function. And the function is going to take integer A and integer B as parameters. These are the two parameters that I have on the parameter line. And it's going to return the addition of them. So I'm going to go return A plus B. And that is the entire function that I'm writing. And inside of the main function, I'm going to call this add function. And I'm going to send it two parameters, which are going to be two numbers that will stand for A and B. So I'm going to call add. And add is going to take 10, and it's going to take 56. There's some two random numbers I decided to add it to send to the add function. And so 10 and 56 will be sent as parameters to the function add. They'll come in as A and B. And the function is going to return A plus B. Now I don't actually have to do it this way. I can actually create variables inside. For example, I can say integer C is equal to A plus B. And then I can return C. But usually you'll see it returning. Usually you'll see it just returning A plus B. It's a little shorter way of doing it. But in any case, both of these two mechanisms will do the same thing. And so in here, this will return, but nothing's going to happen. So nothing gets printed to the screen or anything because I don't have anything being printed to the screen. Inside the function, I can go let's say C out. And then I can say answer, answer is. And then I can put in C, a little space there. And if I do that and I run it, I will see that answer is C will be printed to the screen when I run the function. So if I go ahead and run it, I see the answer is 66. And the answer is 66 because that's 56 plus 10. Now I could also take this here. Let me comment this out, actually. And say down here, integer C is equal to add and this integer C is going to be equal to that. And then I could also see out C from main. And the same thing will happen. But now I'm printing out C, which is the integer variable that I am assigning the return value of. And I don't necessarily need to use C in here, but this is a different C. The name variable, this is a local variable, integer C, that's local domain, it's a different C than this C here. In fact, if I just want to minimize this, I can just take it back to the original code that I had and just go return A plus B, because I don't really need to keep it in a variable anymore. And we'll notice that this will work as well, we're just going to take A and add it to be. And we're going to use the same numbers as we did before, but from main, and here I can even do this here. And you can put anything you want inside the opening of closing brackets. But from main, I get, and what am I going to get? I'm going to get C and C is the adding of 10 plus 56. And if I build it and run it, I should get the same thing. And I do, I said, from main, I get 66. Now, if I wanted to, I can actually just take this little piece here, and put that inside of here instead of C, and not use C at all. And I'm just going to run the function and the function basically is going to, I'm going to get the return value. So this is what they call the function call, I'm calling add, and I'm sending it these two parameters. And the function is returning a plus B. And so the return is what is represented by the function itself. If I do this and run it, I should get the number in there. As if, and I do, it says from main, I get 66, which is the same as we did when we added it or assigned it to a variable. So sometimes you'll see functions being assigned to variables and the values of the variables being printed out. Sometimes you'll see the return line with just the calculation. Sometimes you'll see a whole big old function body, and we'll write more functions as we go through this course. But I wanted to show you the template of the function, what the function looked like, and how you put one together and how you call one. Now, for example, if I didn't put this function above main, and I put it below main, I'm going to have a problem. So for example, if I cut it here, and let's say I want to stick it below, it does not go inside of main, it goes outside of main. So if I put it below, for example, I have, I'm going to get an error message when I run it, because add doesn't exist, it hasn't been defined yet. So if I run this, I'm going to get an error message. And the error message is this little red thing here, and the red thing here, if I looked at the error is going to say that, hey, function is undefined, we don't know the function yet. So there's a thing called a function prototype. The function prototype is simply the first line of the function definition with a semicolon next to it. So if I copy it, stick it here, edit paste, and then put a semicolon at the end of it, at the end of this line, I have what's called a function prototype. This predefined the function so that it's available to be used inside of main, and then later it is written out. So the function body is included, and the whole function is created, but not until after main. The program is read sequentially from line one to line 18. And so if add doesn't exist, it can be predefined. And this is what they call a function prototype, function prototype. And this automatically defines the function, provides a prototype for what it can be used. So the compiler is aware of it. And then the compiler will find the function body in the implementation of the function below main, and then we'll be able to use that code as well. So this one will fix that problem. And now I says from main, I get 66. And then so now we fix that. I have inadvertently done something here. I thought I'd point out as well as this little red dot that I keep. This is going to set a, a stopping point in the program. You want to remove those little red dots. Otherwise, there will be a pause and a break until you hit a key to return when you run it. We don't want to do that. So I removed it. I accidentally clicked on it when I saw the red square, which was an error message. But in any case, our program is working now. We have our function prototype above. We have our function implementation below. Some other people will actually just put the function like I did originally above main. If you want to call it from main, you either have to put a prototype above it or you have to put the actual function implementation above it. So that main is aware of this function. So otherwise, it's going to say, Hey, we don't have an ad function. That doesn't exist. We can't run it. So I hope you have enjoyed this video. All functions work this way. You can have any number of parameters. You only have one return value, however. And you can have multi returns. If we do a recursive function, we'll see in a few minutes. But for the most part, what you've got here is a function call and a function return. Stay tuned for the next video.