In this lecture will refactor our solution to one of the problems we solved in the iteration lesson. The problem is, we want to print a box comprised of asterisks of a user provided width the height. The constraints on the problem are both the width and height need to be between 3 and 20 inclusive, and we need to use functions as much as possible in our solution. This is our nesting in boxes code from the iteration lesson from the previous course, though I have added the function prototype that we developed last time and the body of the function as well. So we're going to start by using this function instead of, doing this, to get the valid width. So let's change our code a little bit, will take our variable declaration and put it here and set it, equal to GetValidInput, with remember four arguments. So here's our prompt string, and a comma, here's our error string and we won't grab that new line with it. Because the function includes a new line after it prints the air string, and the upper bound and the lower bound. And then we can get rid of all of this stuff. And now we'll test it and make sure that works fine. Error checking works fine. And then we enter our height and we get a 5 by 3 box. Now that we know this works fine, we'll say get valid width and height, and we'll do the same thing, to get the height. We can get rid of this variable declaration up here, and of course we want to change the prompts as well. And now you can get rid of all this code. So let's test this. And we got a 5 by 3 box. Now it looks like I need to add a new line here to separate our input from our output, so I'll just do that right here. And we'll run it again, bye bye 3, and we get that new line. So a couple of things here, as you can see, using this function multiple times, makes it so that we have less code in our program, which is great. And, you'll notice I didn't retest the error checking when I called the function to get the height, because we already know that function is working properly. So that's one of the great things about functions, right? Is we can reuse them as many times as we want. Let's add another function, that will handle lots of our printing for us, printing a repeated character a particular number of times. So up here all add another function prototype, this one will be void and I'll call it PrintCharacter, and will pass in two arguments, so the function needs two parameters. We need to know the character we're going to print, and we need to know how many times we should print that character. So there's our function prototype, I'll grab that. And down here at the end, I'll build the function body for it and before I do that, I'll add a comment. Prints the given character NumTimes times and maybe I'll fix a little spelling stuff there. The compiler doesn't care about spelling but the comments are for humans and some humans care about spelling. So the character to print, how many times to print it. You should definitely know how to implement the body of this function, will use a for loop. And I will say, I regularly use int i = 0 as my declaration of my loop control variable in my initialization of my loop control variable. There are other ways you can do this, right? You could use a braced initializer to set i to 0, you could even do this and the default would be a value of zero, so there are lots of ways to set i to 0. I just happened to use this syntax when I do that, and i < NumTimes and i++. The body of our four loop, all we're going to do is print out that character. We have lots of places in the body of the main function to do this. For example, printing the top row, we used what looks an awful lot like a four loop printing out the asterisk with the number of times because we started at one and we said I lesson or equal to it. So let's just say PrintCharacter, the character we want to print is an asterisk and we want to print it with times. We'll test our code and make sure this works, so we'll do a 5 by 3 box, and as you can see the top row is printed fine. So now that we know we can do that, we'll print the bottom row the same way. And I'll run it and test. And everything is working fine. Now, as you've noticed each time I make a small change, I test it to make sure my code still works. That's a really good approach to use both, as you refactor code, which is what we're doing here. We're changing the structure of the code without changing its behavior. But also, even when you just are doing pure development, add a little bit of code and test it,and so on. And that helps when you have a bug,notice I said when not if, when you have a bug, it's easier to find the bug because it's likely not positive, but likely that it's in the new code that you just added. Okay, we still have some more repeated printing of characters because we have here in the middle of the box. We print space a certain number of time and we look carefully and we remind ourselves, that we were printing spaces with -2 times. So we'll change our asterisk to a space, and will change this to with -2. We test again, 5 by 3, and we get a 5 by 3 box. Now we could leave it like this, but I want to add one more function. I want to add a function that prints the box interior, the middle of the box right here, I want to do this in a function as well. And I'll show you why I want to do that after we've done it. So up here we need one more function prototype, Print BoxInterior. And we're going to need two parameters here, we're going to need to know the width and the height of the box. So I'll stay width and height) as my parameter, and of course again to have a space in the function name. And I'm going to copy this prototype. Down to the end, and I'll add a body, and I'll add a comment, prints the interior of the box, and of course we have a box width and a box height. The code we need for this, is already implemented right here. And because I didn't change the names at all, I'm still using width and height, we should be able to run just fine, you're excited, you're ready to run and so you control that 5. And you say 5 by 3 and you're sad because you only get the top and the bottom of the box, a reminder, just writing the function is not sufficient. We have to actually call it as well providing the width and the height for the box, and now when I run it I get a 5 x 3 box again. Now you may be wondering why I decided to write this function even though I only call it once. And the big reason is, we'd really like our main function to give us a high level view of how we're solving this problem. And by doing it this way, it's pretty clear in our main function, we have these high level chunks of functionality included in the main function. But we're letting our separate functions do the heavy lifting to implement the actual functionality. So we know we're getting valid width and height and then we print the top row and then we print the middle and then we print the bottom row. So our main function gives us a high level of what's going on. And then our other functions handle most of the real work. To recap, in this lecture, we re factored our solution to a problem from the iteration lesson and we improve that solution by using functions.