c++typesvoid

What is the purpose of functions which return void?


void f() means that f returns nothing. If void returns nothing, then why we use it? What is the main purpose of void?


Solution

  • 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 void keyword as "return type".


    Keep in mind that void serves also other purposes; in particular: