At this point, we know how to pass data into a function and return data from a function. In this lecture, we'll write a function that gets valid user input. So our problem is that every chunk of code that we write to get valid user input looks a lot like every other chunk of code that we've written to get valid user input. The solution is to write a reusable function that gets valid user input for us. So let's go do that now. Our function will return an int, and we'll call the function GetValidInput. And this function will have four parameters. So we're going to see a parameter list that has more than one in it. We'll have a string for the prompt. So I'll call that PromptString and our parameter list is a list of comma separated parameters. So I will also add an ErrorString. So when the user inputs an invalid input, we can print that error and prompt again. We'll have a lower bound for the valid values that we're trying to get from the user. And we'll have an upper bound for the valid values were trying to get from the user. So there's my function prototype. I'll grab it, and bring it down here. And now I'll add a comment above it saying what it does. Gets a valid input from the user. And even though these parameter names are self-explanatory, I'll add a description, a brief description of each of those. And finally, we'll return the valid input value. Within the body of our function, this is our standard pattern for getting valid user input. So I'll just type that in and then we'll come back. Okay, so I've declared a local variable here in the function and I'm going to print the prompt string and read in the value. While the value is less than the provided lower bound or the value is greater than the provided upper bound, I'll print the blank line. I'll print the error string, I'll reprint the prompt string and I'll read in the value. Once this while loop is done, that means that value has a valid value. So I'll return it because I promised I'd return an int from this function. Let's see how we can call this function multiple times from our main function. So let's first say int NumScores. So we're going to ask the user how many scores they want to enter. And we'll set that value equal to GetValidInput using a whole bunch of arguments here. Our first argument is the PromptString. So how many scores do you want to enter. And we'll give them a range, 1 to 10. Our next argument is our error string. So number of scores must be between 1 and 10. Our next argument is our lower bound and our final argument is our upper bound. We'll just print out the value we get here. And we'll get rid of this code momentarily but we want to see that we actually get a correct value returned from the function. When I run the code, first, I'll enter some invalid possible number of scores, and then finally I'll enter a valid one. And we do get one back. So that's great. But we've added extra codes to do this. Here's where we get the real benefit. We'll say (int i = 0; i < NumScores; i ++). And we'll read in each of those scores. Now, I will say typically, right, we'd put this into an array or an array or vector container. I'm just going to read them in to show the reuse of this function. But that's the real power of functions that we can reuse them. And by providing different arguments, we get different behavior. So I'm going to say here in the body of my for loop, I'm going to say Score. So I'll just say Enter Score, and a score can be 0-100 on a typical test for example. So that will be my prompt string. And here I can say Score, Must be between 0 and 100. So with different error string that I'm using. And then of course my lower bound and upper bound change as well. So now when I run my code, first, I'd provide the number of scores I want to enter and showing you. Of course, that does the error checking. And let's say I'm going to enter three scores. So now I ask for a score, and I'll say it's -1, or it's a 101. And then I'll enter a 100, and 99, and 98. And I'm all done. So a huge benefit of functions is once we set up the function header to say what data type is returned, what's the name of the function and what are the parameters for the function. We can call that same function with different arguments to get different behavior out of the very same function. And that is a huge win. To recap, in this lecture we wrote a very useful function that will get valid user input and you can reuse this function over and over again in your code.