-
void
what is void and where do i use it?
example
void gameplay() i dont get why it cant just be int gameplay()
and also could you just put this in every program or are there certain sircumstances and what does it do?
int main( int argc, char* argv[])
ps.the codes were to small to bother with code blocks...
-
void signifies lack of type.
if you have int gameplay(), you must return a value. If your function has nothing meaningful to return then int gamplay() is at best wasted effort (an extra "return 0") and at worst confusing to users (what the hell do I do with the int returned from gameplay???)
basically void signifies you don't care about the return value/arguements.
-
ok and why bother with the () about void gameplayer
but int can always be used its just pointless at times and how do i know when i dont care about the return value
-
Because you aren't returning an int, so there is no reason to. The void data type is used when the function returns nothing. If you're passing data to a class to store, then you would return void because the class doesn't need to return any data. Or if you have a function that just outputs a line of text... it doesn't need to return anything.
Void can also be specified as a pointer which can point to any memory address regardless of the value. It also can be used to implicitly specify that there is no arguments being passed to a function, but is not required in C++ as it is in C.
Code:
// Example 1
void myClass::setData(int d) {
this->data = d;
return; // This line is optional
}
// Example 2
int data = 5;
int *myIntPtr = &data;
void *myVoidPtr = myIntPtr;
char info = 'A';
char *myCharPtr = &info;
myVoidPtr = myCharPtr;
// Example 3
int myClass::getData(void) {
return this->data;
}
-
thanks ALOT,too bad i could never figure out a use for pointers :p
-
Just keep on learning... soon their usage will hit you in the face like a ton of bricks.
-
lol,yeah i've been noticing that alot now.
especially with switch statements,i use them all the time :D