Void?

Hi, sorry for asking such a basic question, but when is it necessary to use void? I have a book and it says very little about void except in a table on data and for it says "none".
Void is usually used in functions and it returns no value(not quite sure what it really means)...

Example:
1
2
3
4
5
6
7
8
9
10
11
void explain()
{
    cout <<"\tCreate a username and password first. Note, when\n"
         <<"\ta new set is created, make sure that you have not created\n"
         <<"\tthe username before, and that this uses the stupidest\n"
         <<"\tform of security ever created. DO NOT STORE CONFIDENTIAL\n"
         <<"\tSTUFF because any baffoon can crack this program. Also, you\n"
         <<"\tcan't make a username including spaces, as the program glitches badly.\n"
         <<"\t--PS: you can only store text.--\n\n\n";
         return;
}


Ps: Ignore the text, I directly copied it from one of my own projects.
Last edited on
void as a return type for a function means that the function does not return any value- so the function does not require a return statement:

1
2
3
4
void ShowMenu() {
//std::cout statements to print the menu
// no return statement
}


It can also be used to specify that a function does not have any parameters, but this has not been a requirement for C++ for a long time. You can just leave the parentheses empty as in the code snippet above.

There is also pointer to void, but that is mainly a C language thing as far as I can tell. I don't have a great deal of knowledge on that though.

Hope this helps.

@inc0001
As I said, line 10 in your code isn't needed. We were posting at the same time.
Last edited on
Not only is it not required void CANNOT --- return any time unless the compiler is configured
Not only is it not required void CANNOT --- return any time unless the compiler is configured


It's unclear what you're trying to say here. If you are trying to say that a compiler must be specially configured for a function with a void type to return, you are mistaken.

And as far as not being required for a function that doesn't return a value, yes it is required.
The return statement can be especially useful if the function may exit at multiple points.
Last edited on
Topic archived. No new replies allowed.