using functions

I am learning about using functions in programs and Im not sure how wrong/right I am for this program which takes the age of 3 people, displays the oldest and calculates the average, also using validation for the age.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
using namespace std;

int main()

{
	int age1 = 0;
	int age2 = 0;
	int age3 = 0;
	double average = 0;

	cout << ("Enter first age");
	cin >> age1;

	cout << ("Enter second age");
	cin >> age2;

	cout << ("Enter third age");
	cin >> age3;

	if (!isValidAge(age1)) // check value is within a sensible range
	{
		cout << "Age 1 is not valid";
	}

	else if (!isValidAge(age2))
	{
		cout << "Age 2 is not valid";
	}

	else if (!isValidAge(age3))
	{
		cout << "Age 3 is not valid";
	}

	else
	{
		oldest(age1, age2, age3); // display highest age value
		average = averageAge(age1, age2, age3); // calculate average
		cout << "Average age is " << average;
	}

	return 0;

}

bool isValidAge(int num1)
{
	if (num1 <= 0 || num1 > 110)
	{
		return false;
	}
	else
	{
		return true;
	}

}

void oldest(int num1, int num2, int num3)
{
	if (num1 > num2 && num1 > num3)
	{
		cout << num1;
	}
	else if (num2 > num1 && num2 > num3)
	{
		cout << num2;
	}
	else
	{
		cout << num3;
	}
}

double averageAge(double num1, double num2, double num3)
{
	double average = num1 + num2 + num3 / 3;
	return average;
}


It doesn't compile, as you have no explicit interface for your functions at point of call. (Either use function prototypes or put those function definitions before main.)

Function oldest() will be wrong if the first two ages are equal to each other and greater than the third (for example).

averageAge will be wrong - you are missing brackets.

Functions like this would be better returning a value, to print out or not in main(). You have done this for two of them, but not oldest().

I assume you are imposing euthenasia at age 110!
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";
Last edited on
Topic archived. No new replies allowed.