Why do i need to use void when it does not return a value? |
Well, let's say you have a function declared like this:
we can see this function will return a value of type int.
What if we wanted a function which did not return any value at all? Well, you might think it could be declared like this:
in other words simply omit the return type.
But - there are at least two problems with that. One, that declaration looks exactly the same as when we call the function, so it is not clear what is the intended meaning. Secondly, historically if the return type was omitted (in the C language), the compiler automatically assumed that the function would return an int.
So instead we use the declaration
this makes it clear that it is a declaration rather than a function call, and we know for certain that the function will not return a value. Think of the word 'void' here as meaning 'nothing at all, not even empty space'.