Oops, I guess I just used a "while" loop and not a "do-while" loop. A do-while version is attached below. If you compare the two, you might see why I think my original one is better.
Anyway, I'm sure your professor has gone over functions before, but I'll try and explain it myself, maybe a different type of explanation will help.
You can think of functions as little autonomous programs that run in the middle of your entire program. Each one of these programs (functions) can have its own inputs and outputs. Inputs are called "arguments", and outputs are called "return values". In C, your function can have as many arguments as you want, but only one return value.
The function prototype you were given looks like this:
int reducedSum(int value)
Let's break that down a bit:
int -- this is the first part of the prototype. It's called the "return type". This is the type of value that this function will return. Your function will return an integer.
reducedSum -- this is the name of your function. You give each function a name so that you can use it later. I'll get to this more in a minute.
(int value) -- this is a list of all the arguments that your function takes. Your particular function only takes one argument, an ineger named "value". If your function took more than one argument, then thier names would all show up in the prototype between the parentheses, seperated by commas.
So, before we look at the code inside your function, let's look at how you run your function. Running a function is called "calling a function".
To run your function, all you have to do is type its name, and give it the appropriate arguments. The arguments can be either variables, or constants.
Here's an example of how we run your function.
reducedSum(123);
That will run your function using the constant value 123 as its argument. You could also give it a variable as an argument. The only thing that you have to do is make sure that whatever you give it is an integer. Here's an example with a variable:
int x = 321;
reducedSum(x);
That's how we get data *into* your function, but we also talked about getting output from your function, using the return value. So far we've ignored the return value, but I'll show you what we can do with it. Remember that the return value is an integer, so we can use it just like any other integer. Here's an example.
int x = reducedSum(123);
now, when that line of code runs, your function will run as if it was its own little program, and when it's done it will return an integer. Whatever value that integer has will then be stored in your variable x.
Now let me show you how the function works inside. I'll use a simpler function as an example, rather than the one for your assignment. I'm going to make a function called "timesTwo" that multiplies a number by two. It will take a sinlge integer argument, and return an integer, just like your reducedSum function. Here's the function:
int timesTwo(int value){
int valueTimesTwo = value * two;
return valueTimesTwo;
}
Now, here's what happens when you call this function. Let's assume you called it similar to our earlier example, with this line:
int x = timesTwo(123);
When your program sees the name of the timesTwo function there, it starts running the code inside timesTwo. The first thing it does is figure out the arguments.
Your fnction only has one argument, named "value". You passed in the number 123 as that argument. What this means, is that when the function begins running, the number 123 gets copied into the variable "value". Then you can use "value" just like any other variable.
You can see that the first line of our function just multiplies "value" by two, ans stores that value in a variable called "valueTimesTwo".
The next line of our function returns a value. Our function returns "valueTimesTwo", so that when you go back to where we started the function with the line "int x = timesTwo(123)", the value of "valueTimesTwo" gets copied back to the original location, as if we had done this:
int x = valueTimesTwo;
And there you are, your function returned a value.
Now you might be wondering why this is useful. The reason is that it allows you to use the same code more than once without rewriting the entire thing. Imagine you created a vaery complex function that contained 500 lines of code. Pretend this function could return a persons driver's license number if it was given his social security number.
Once you have the function, you can do this:
int ssn1 = 123456789;
int ssn2 = 123234567;
int person1dlnum = getdlnum(ssn1);
int person2dlnum = getdlnum(ssn2);
and now you've run that 500-line function twice, without having to write the 500 lines of code twice. All you had to do was give it two different arguments.
And one last thing - the name "function prototype" is jsut used to mean the description of the function name, it's return type, and it's list of arguments. It's like a blueprint for what the function will do, but doesn't include all of the actual code for the function. The actual code for the function is called the "function body" and is located inside the curly braces ("{}") that follow the function prototype.
That tooka while to type. I hope it's helpful
