Generally I think the outline is a bit off. It seems unnatural to me to ask the user for the score, validate and return the value all within the function. It seems more natural to ask the user for a value and then send that value to a function for validation. However, a spec is a spec and that's what you've been give so let's deal with it.
So you don't understand the return function?
Ok,
return
isn't a function. It's a keyword that will send whatever value it's used with back from the function that it's in.
Let's take a look at a function:
1 2 3 4
|
int Double(int x)
{
return x * x;
}
|
That's a simple function that will double whatever value is passed into it (the parameter) and return it back from the function. So we'll break this down. The first
int
there is the return type. This lets the program know that this function should return an integer value.
Double
is the name of the function. Anything within the () operators are the parameters - the values that are passed in and used within the function. In our example, we're passing in an integer, which, within the scope of the function, will be named 'x'.
Functions don't need to take parameters. Nor do they need to return values. A function that doesn't take parameters will look like this:
1 2 3 4 5 6 7 8 9 10 11
|
int MyFunction()
{
return 0;
}
// or
int MyFunction(void)
{
return 0;
}
|
Down to your example. Your function there has parameters. Five of them to be exact. They shouldn't be there as per the program specification. The function calls are in line with the specification but don't match any of the functions you've written.
Here's a brief outline of how I'd go about doing this:
1 2 3 4 5 6
|
// Create an integer returning function, without parameters
// Create a variable in there to store the user input
// Get the input for the user
// If it's valid, return that value. If not, return some error value.
// In main, create an array of integers for each test score
// Loop through the array, calling that function for each score
|
Give it a go, see how it goes. If you get stuck implementing any of that, pop back with what you've got so far.