if it returns nothing then what would this result in, 0 or space?
Code:#include <iostream> void test() { return; } int main() { cout << test(); }
if it returns nothing then what would this result in, 0 or space?
Code:#include <iostream> void test() { return; } int main() { cout << test(); }
also, what happens when you do this with while or if?
Code:while/*or if*/(1; 0+1; /* and so on */ ) {}
probably, maybe i will try it
you're right, i just tried it
void doesn't even have a size, so it is nothing. I don't see a meaningful difference here. It is equally erroneous to state that just because a function has no value to return, that it will never return because "you cannot return void". The only circumstance any function does not return would be because it does not reach an exit point. Compilers do complain about unreachable code in a couple of ways.
Blatant mistakes don't really prove your point. This is the same error as any invalid type conversion.Code:void foo() {} int main() { int x = foo(); }
My point is that the compiler treats it as something. While in your return statement you don't put void to return nothing, you simply leave it empty.
It is my understanding or interpretation of the whole anyway. "void" can be seen as a type.
(Otherwise why would void* be legal? And why not void in the parameter list be legal?)
Void is a curious thing, yet there. Semantics.
I would think the inverse -- if the compiler treated void as something, then you would need to create it to appease the compiler. You don't need to do that, and thank goodness, because you can't. Meanwhile void* is something, because it's a data pointer. Symmetry is for the deluded people.My point is that the compiler treats it as something. While in your return statement you don't put void to return nothing, you simply leave it empty.
I'm not saying void isn't a type, but saying it is nothing would be perfectly correct. After all, returning void means returning nothing. In common vernacular it's just harmless.
thats a good way to put it,
why would you have main return anything other than 0 though?
To let the caller know how the execution went. Let's say you wrote a program that just output the contents of a file to the console. On success, your program would return 0. If your program failed to open the specified file then main() could return 1, letting the caller know that something went wrong.
If you understand what you're doing, you're not learning anything.