You are misunderstanding return types.
The return type specifies what a function call resolves to in an expression. In English, this means that when you call a function, the function call effectively gets "replaced" with whatever the function returns.
(note it's not really "replaced", I'm just illustrating the concept here)
Here's a simple example:
1 2 3 4 5 6 7 8 9
|
int func()
{
return 5;
}
int main()
{
cout << func();
}
|
Since 'func' returns 5, this means that the function call is "replaced with" 5.
So
cout << func();
becomes
cout << 5;
. Therefore the above code would print 5.
A slightly more complicated example:
1 2 3 4 5 6 7 8 9 10 11
|
int add(int a, int b)
{
return a+b;
}
int main()
{
int foo = add(4,9);
cout << foo;
}
|
Our 'add' function adds the two numbers together, and returns the sum. Therefore
int foo = add(4,9);
becomes
int foo = 13;
because the function will return 13.
void functions
have no return type, so they don't get replaced with anything. Here's an example:
1 2 3 4 5 6 7 8 9 10
|
void func()
{
cout << "Example";
// not returning anything because this is a void function
}
int main()
{
int foo = func(); // ERROR, see why below
}
|
That line of code will give you an error because func() doesn't return anything (it's void!), so you can't use it in an expression like that. Think about it... what would you be setting 'foo' to in that code? You can't set foo to nothing. That's why it's an error.
That's basically what you're doing. You're calling a void function and using it as if it returned something. You're doing this:
1 2 3 4 5 6 7 8 9
|
void func()
{
// stuff here
}
int main()
{
cout << func(); // ERROR
}
|
What do you expect cout to print? 'func' doesn't return anything, so how can cout print anything?