First, is the compiler happy?
In function 'int main()':
21:22: error: 'isValidAge' was not declared in this scope
38:26: error: 'oldest' was not declared in this scope
39:40: error: 'averageAge' was not declared in this scope |
No.
Identifiers have to be
declared before they are used.
On line 21 we do not know yet, what the 'isValidAge' is. We have not seen it yet. If the
definitions (aka implementations) were before main(), then they were seen. Function definition acts as declaration.
However, it is better to insert just declarations before main:
1 2 3
|
bool isValidAge(int num1);
void oldest(int num1, int num2, int num3);
double averageAge(double num1, double num2, double num3);
| |
On a larger program the declarations would be in
header files that are included (like you include 'iostream') and the implementations in separately compiled source files (.cpp-files).
That said, you define and call functions ok.
The averageAge does not really need the local variable:
1 2 3 4
|
double averageAge(double num1, double num2, double num3)
{
return num1 + num2 + num3 / 3; // same result as in yours
}
| |
However, now that I look at it, what is the difference of these two:
1 2
|
num1 + num2 + num3 / 3
(num1 + num2 + num3) / 3
| |
The logic for "find largest value" can be done many ways (and some are tricky).
Consider though that your 'oldest' is limited to always display a value and if caller would need that value (for other that show), then they would need different function/solution.
Note that these parentheses do not improve the program:
1 2 3
|
cout << ("Enter first age");
// is same as
cout << "Enter first age";
| |