You aren't quite there yet.
Here is how you call any function:
1 2 3 4 5 6 7 8 9
|
void function() {//this is just the declaration / definition
/*do something*/
}
int main() {
//this is where you call the function
function();//notice the parentheses
return 0;
}
|
Similarly, if I have this:
1 2 3
|
int return_five() {
return 5;
}
|
And I want to print the value that is returned by this function (namely five), then I would do this:
1 2 3 4 5 6
|
int main() {
std::cout << return_five() << std::endl;//again, note the parentheses
return 0;
}
|
If I didn't have the parentheses, the compiler would assume that I'm trying to print a variable with the same name.
1 2 3 4 5 6
|
int main() {
std::cout << return_five << std::endl;//error, there is no identifier "return_five"
return 0;
}
|
If your function expects arguements / parameters, then you
need to supply them to the function when you are calling it.
Let's say I have this function, which adds two integers together, and returns the sum:
1 2 3
|
int sum(int a, int b) {
return a + b;
}
|
I need to give it two parameters when I call it. It wouldn't make sense to call a function that adds two numbers together, and then to only give it one number.
1 2 3 4 5 6
|
int main() {
std::cout << sum(1, 2) << std::endl;//prints the sum - three in this case
return 0;
}
|
One final thing. This has to do with scope.
In C++, most of everything is stack-bound (unless you're dealing with dynamic memory, I won't get into that). When I say "stack-bound", I mean that things like variables only exist within a certain scope (area) of your code, and if you attempt to access them anywhere else, it's very likely that you'll get a compilation error.
To illustrate this better, here I have a main function:
1 2 3 4 5
|
int main() {
int a = 10;
return 0;
}
|
Which doesn't really do anything exciting. It does, however, instantiate an integer by the name of "a", and gives it a value of ten.
Once the main function terminates, that integer dies, and ceases to exist. You might say "Duh, because that's when the program ends". This is true. Perhaps a better example would be this:
1 2 3 4 5 6 7 8 9 10 11
|
void function() {
int a = 10;
}
int main() {
function();
a = 20;
return 0;
}
|
What do you think this code does? If you answered : "It will change the value of "a" to twenty", then you're wrong. It shouldn't compile. The reason is because the integer "a" only exists within the scope of
void function(). The main function has no idea what "a" is.
Therefore, your snippet:
1 2 3 4 5
|
float regpay (float grosspay, float hours)
{
regpay = grosspay * hours;
return regpay;
}
|
Is incorrect, and shouldn't compile. In the regpay() function, there is no regpay variable. Therefore, this should result in an undeclared identifier error.
I once summed up the concept of scope a lot nicer in another thread - I'm trying to find it right now. I'll edit this post once I find it.
As far as mismatched function prototypes and definitions go, they need to match 100%. Here's how the regpay() prototype should look:
float regpay(float grosspay, float hours);
And before anyone goes correcting me, yes I realize there are unnamed / anonymous parameters. OP should not be concerned with those ATM.