void f() means that f returns nothing. If void returns nothing, then why we use it? What is the main purpose of void?
void f() means that f returns nothing. If void returns nothing, then why we
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When C was invented the convention was that, if you didn’t specify the return type, the compiler automatically inferred that you wanted to return an
int(and the same holds for parameters).But often you write functions that do stuff and don’t need to return anything (think e.g. about a function that just prints something on the screen); for this reason, it was decided that, to specify that you don’t want to return anything at all, you have to use the
voidkeyword as “return type”.Keep in mind that
voidserves also other purposes; in particular:if you specify it as the list of parameters to a functions, it means that the function takes no parameters; this was needed in C, because a function declaration without parameters meant to the compiler that the parameter list was simply left unspecified. In C++ this is no longer needed, since an empty parameters list means that no parameter is allowed for the function;
voidalso has an important role in pointers;void *(and its variations) means “pointer to something left unspecified”. This is useful if you have to write functions that must store/pass pointers around without actually using them (only at the end, to actually use the pointer, a cast to the appropriate type is needed).also, a cast to
(void)is often used to mark a value as deliberately unused, suppressing compiler warnings.