Void Functions in C++

I'm learning C++ right now in class. We need to use a void function on our next project, but the only thing I understand about it is that you use it to call a value where one is not entered. I tried using void twice in this code. If someone could tell me what i'm doing wrong, it would be appreciated :D

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
  #include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main ()
{
	void DisplayTitle();
	const int height = 8,
			  can = 250;
	const double price = 12.99;
	int length,
		width,
		sqft,
		side,
		ceiling;
	double	cost,
			ttlcan;

	void DisplayTitle()
	{
	cout << "\n           Chesapeake Remodeling Company";
	cout << "\n                    Paint Costing";
	cout << "\n        -------------------------------------";
	}


	void GetData(){
	cout << "\n\n    Enter length in feet (0 to stop)...";
	cin >> length;
	}
while (length > 0){
	cout << "    Enter width in feet................";
	cin >> width;
	DisplayTitle();
	side = length * height;
	ceiling =  width * length;

	sqft = (4*side) + ceiling;

	ttlcan = sqft / can;

	cost = ttlcan * price;

	cout << "\n     Square Footage = " << sqft;
	cout << "\n     Cans           = " << ttlcan;
	cout << "\n     Cost           = " << cost;

	DisplayTitle();
	GetData();
}
	system("pause>nul");
	return 0;
}
The first thing I see that's wrong is you are putting your functions inside main.

They should not go in main, but should go outside of it:

1
2
3
4
5
6
7
8
9
10
11
12
13
void DisplayTitle()
{
    cout << "\n           Chesapeake Remodeling Company";
    cout << "\n                    Paint Costing";
    cout << "\n        -------------------------------------";
}


int main()
{
    DisplayTitle();  // calls the function
    //...
}
closed account (o3hC5Di1)
In addition to Disch's comment, I would like to add the following: void is the return type of the function. Functions can return values, for instance, a funciton that adds up integers returns an integer:

1
2
3
4
int sum(int a, int b)
{
    return a+b;
}


Now, not every function needs to return a value, it can just do something without reporting back to where it was called. In such cases, we declare the function as void. For instance, your DisplayTitle() function, just prints text to the screen, it doesn't need to return any values to the part of the program that called it.

All the best,
NwN
It is not proper to refer to a function with return type void as a "void function", but this is just a pet peeve of mine.
Thanks everyone for the help. I just had the void in the main. :)
Topic archived. No new replies allowed.