# Thread: Turn program into a void function

1. ## Turn program into a void function

I started out with making this program and figured it would be easier to turn it into a void function, how do I do that. I'm not catching on for some reason. here is my code

Code:
```#include <iostream>

using namespace std;

int main()

{
float A;
float B;
float C;
float D;
float E;
float Average;
float Sum;

cout << "Please enter the 1st integer ";
cin >> A;
cout << "Please enter the 2nd integer ";
cin >> B;
cout << "Please enter the 3rd integer ";
cin >> C;
cout << "Please enter the 4th integer ";
cin >> D;
cout << "Please enter the 5th integer ";
cin >> E;

Average = A + B + C + D + E;
Sum = Average / 5;

cout << "The average of the following numbers are " << Sum << endl;

return 0;
}```

2. Why would it be easier? This function already does exactly one task (accepts 5 numbers, averages, and then outputs). It doesn't clearly break into pieces that could form standalone functions very easily. I mean, there are some possible improvements you could do, by adding more features for example, but nothing simple that would make it more elegant.

Also, why on earth do you do this??:

Code:
```	Average = A + B + C + D + E;
Sum = Average / 5;```
The first line computes the sum of 5 numbers, but you name it "Average", the second computes the average of those numbers but you call it "Sum". Unless you like confusing readers, why not call the sum "Sum" and the average "Average" and not vice versa?

3. A void function is basically a function that has no return type, ie. does not return anything. Therefore, if your function is not going to return anything, I would do something like this.

Code:
```#include <iostream>

using namespace std;

void GetAverage(void);

int main()
{

return 0;
}

void GetAverage(void)
{
float A;
float B;
float C;
float D;
float E;
float Average;
float Sum;

cout << "Please enter the 1st integer ";
cin >> A;
cout << "Please enter the 2nd integer ";
cin >> B;
cout << "Please enter the 3rd integer ";
cin >> C;
cout << "Please enter the 4th integer ";
cin >> D;
cout << "Please enter the 5th integer ";
cin >> E;

Average = A + B + C + D + E;
Sum = Average / 5;

cout << "The average of the following numbers are "    << Sum << endl;
}```
However, I would personally write it like this.

Code:
```#include <iostream.h>

float GetAverage(int NumItems = 5);

int main()
{
cout << "\nThe Average is " << GetAverage();

getchar();

return 0;
}

float GetAverage(int NumItems)
{
int InVal, Average = 0;

for(int i = 0; i < NumItems; i++)
{
cout << "Please enter integer " << i + 1 << ": ";
cin >> InVal;

Average += InVal;
}

return (float)Average / (float)NumItems;
}```
Oh, use namespace and that forgot it.

4. Based on your earlier thread, what you need to do is create a new function called GetMeanof() to find the average. Then call this function from your main() function.
Code:
```#include <iostream>

using namespace std;

void GetMeanof( int n, float &mean );
/* in */ /* out */

int main()
{
float mean;

GetMeanof( 5, mean );
cout << "The average of the following numbers is " << mean << endl;
return 0;

}

void GetMeanof( int n, float &mean )
/* in */ /* out */
{
float A;
float Sum = 0;

for (int i=1; i<=n; i++)
{
cout << "Please enter float number " << i << ": ";
cin >> A;
Sum += A;
}
mean = Sum / n;
}```

5. thanks for all the help. I was looking at all of themposts and I see that Iw as not using the for( calculations. I was trying to make them one by one vs making it a shorter version. Thanks for the help everyone.

Bryan