-
Void? Help me!!!!!
I've been making lots of progress in C++ in the last couple of days.
But when I got to the part on the tutorial about switch case, it introduced the word "void"
Because there is no explanation, I don't get it.
Will somebody please help?
Cprogramming.com Tutorial: Switch-case structure
Code:
using namespace std;
void playgame();
void loadgame();
void playmultiplayer();
int main()
Thanks a bunch
-
void used as a return type basically means that a function will not return anything. In this snippet, it simply means that those 3 functions aren't returning anything.
-
what do you mean by not returning anything?
-
Not returning anything means that the function ends without returning a value to the function that called it. Take this for example
Code:
int Foo(int bar)
{
return n + 5;
}
Here we said Foo is a function that takes an integer and returns an integer. Now lets say we want to write a function to print something to the screen. We don't need the function to return a value like the previous function did. All it needs to do is print a message so you would write
Code:
void Print()
{
cout << "This is a message.";
}
Now we have the function print. It doesn't take in any parameters and it doesn't return anything after it is called. You do not need a return statement because execution ends at the end of the function because it is void. You can add a return statement but it is not really necessary.
Code:
void Print()
{
cout << "This is a message.";
return;
}
-
The previous lesson on functions does make some comments about void functions.