Hi, in this segment we will be talking about functions in C++. Now the basic structure. Of a function we actually looked at earlier when we talked about the main function. Okay, but we'll go back to that now. Now, each function has a name. And. There are inputs. Possible inputs I should say. And possible outputs. And then there's the body. And if you do have an output, then you would return. The output at the end of the function. Okay. So let's look at some examples of functions here in the code. Let's say I want a function that will calculate, for example, the area of a triangle. And so, I would have an output, and it would be a double, which would be the area. I will name it triangle_Area. And I'd actually have two inputs that I would need. I would need the base, which is a double. And I'd need the height, which is also a double. Notice I've separated the inputs with a comma. And notice that I've given a name to the inputs, but the output doesn't have a name, okay? It will know, C++ knows the value output because at the end of my function I will again say return whatever value I want it to return. Notice with this format I can only return one output, okay? I have my curly braces which define the body of the function, and as we talked about before, it also defines the scope of any variables that I define within the function itself, okay? So I will create a double, a real number area. And the value of area is one-half. I could do 0.5 or I could do 1./ 2. Remember to put the decimal. Otherwise, 1/2 will give me 0. Right? So watch out for that. So I'll make that 1./ 2*base*height, okay? And then, I just return area. Okay, so now I'll go down into the main function. And. I'll print that to screen. Actually, I'll create a double here. Area. Notice, I've declared area twice, I have a double here in my main function, and I have a double area within my triangle area function. But again, they're both within their own respective scopes, so it's okay. They don't see each other, all right? I've created double area there. I'll say area = triangle_area, and I'll put in, just for an example, 2 as the base and 3 as the height. And I'll print that to screen. So we can see what it gives us, okay? My source code is saved, and so I will make run. And it prints us screen three. One-half the base which is two times height which is three, okay? Gives us three. All right, we can actually. Save some space in this function. Instead of creating the double area, defining what the value of area is in the function, and then returning the value of area, I can do that all within one line. If I want to simply say return, 1./2*base*height. And if we run that, again you see it gives us the same value of three. Now, looking back here at the top, I'm passing in values for base and height. You could consider these as being readonly values, readonly inputs, you could say. What's actually happened is in the function I'm copying the values of my inputs base and height and using that within the function to perform some calculation. Okay, there's another way. That's actually called passing by value, and passing them the value of the variable but not the variable itself. I can't modify based on height outside of the function itself. However, sometimes it's nice to be able to do that. For example, if you have multiple outputs that you want, then you can pass in an input by reference which allows you to change that input, and you can change multiple inputs at the same time. So let me create another function here to demonstrate that what passing by reference involves. So here, instead of returning a double, I'm not going to return anything. So I will put void, and I'll pass in double base, double height. Both of those I'll still keep as readonly, but I will also pass in a double &area, and I'll pass up by value. Remember from our discussion on pointers that the ampersand refers to the address or the reference of a variable, okay? So I'm passing this in by reference. And so instead of saying return 1./2*base*height, I'll just say area = 1./2*base*height, okay? And now the value of that input has actually changed within the main function itself. And now instead of saying area = triangle_area, oh I also need to change the functioning. So I'll call the first one triangle_area1. This one's triangle_area2. So for triangle_area2, I'll still pass in 2 and 3 for base and height. But I'll also pass in a double area. And notice I haven't set anything equal to triangle area because that function is void. It won't return anything. But it will modify the value of area that I'm passing in, okay? So now I can run that. And I still get the same value of three. Okay, now what if, I mentioned before you might want multiple outputs. So. What if, for example. For a square. I just want some generic information. I want to pass in this length. But I want to get out of it both the area and the perimeter. You notice I couldn't do this. As easily. I mean I could maybe do it as an output of vector, a vector of doubles. And the first component of the vector could be the area. The second component of the output could be the perimeter. So that would be one way. This is another way to do it, which I think is a little bit cleaner. Okay, and so here I would say that the area = length*length, and the perimeter = 4*length. Okay. So now I need a double perimeter. Now notice I've, just to make a point, I'll change these names here. They don't have to be the same. My inputs within main don't have to have the same variable name as what's defined in my function inputs, okay? So I'll change these names just to make that point. So I'll input those into square_info. I'll pass in a length of 3, for example. Pass in Area1 and Perimeter1. And print those to screen for us to see. Okay, so my file is saved, and so I can run it. And it gives me an error here because I misspelled length. So these. These error statements are really helpful. And usually they can tell you exactly where you need to go, where the mistake is in your code. Okay, so now that is saved, and. It looks like I also misspelled Perimeter there. There, okay. Now we'll run it again. All right, 9 and 12, so 3 times 3 is 9, and 3 times 4 is 12, giving us the area and the perimeter. So that's an example of what you might use a function for. Now here my functions have all been numerical. But remember, functions in C++ aren't necessarily a mathematical function. [SOUND] So let's say I could create a function to, that would print a vector, the components of a vector, to my screen. And it would be a void. And I'll just call it print_vector, or print_vec. And I'll take as an input, a standard vector. Of doubles. And I'll call it output, or out. All right, and then within this function I just want to do a for loop, for(int i = 0; i<out.size(); i++. And then I would then print to the screen out[i]. Std::endl;. Okay, so that would be an example. Of a function that's not a mathematical statement. It's just something to make the main body of my function a little bit cleaner, or the body of my main function to be a little cleaner. So down here if I have a vector declared, a vector of doubles. going to notice in this case because the input to my function is a vector of doubles. That's, it won't work on a vector of ints, for example. And I'll do a length 3, components 1.2, okay? And now I just call this function print_Vec. I input. Uses an input test. And as I run it. There you can see it. Use the function to print all the components of the vector. Okay, so that is just to show you that a function doesn't have to be a mathematical statement. We use functions usually to make the body of the main function a little bit cleaner, a little bit easier to read. And, often, if there's something that you're repeatedly doing within your body, you'd probably want to make a function, so that you've only to find the operation once, and you can just refer to it again and again. Okay, so that will conclude our discussion on functions. And in the next segment we'll move on to talking about classes.