Erm...
I didn't understand about using void in functions.
I know that void doesn't return value.
but i am not clear how to use it in function.
Can anyone please show me an example how to use void in function?
Thanks in advance!
Printable View
Erm...
I didn't understand about using void in functions.
I know that void doesn't return value.
but i am not clear how to use it in function.
Can anyone please show me an example how to use void in function?
Thanks in advance!
Quite simply, if you cannot return a meaningful value from a function, do not return anything. Declare its return type void (although, void is not a type). For example, a function that prints an int:
Code:// Nothing can be meaningfully retrun so, it is void...
void foo(int x)
{
std::cout << "The int: " << x << std::endl;
}
thanks for reply :D
Some people also like to use void to explicitly indicate that a function has no parameters. So the two functions declarations below are essentially the same:
void foo();
void bar(void);
They are the same in C++ since a prototype is a declaration. In C, however, void foo() is a declaration and void bar(void) is a prototype. A prototype is a declaration but a declaration is not a prototype.
Remember, main never returns void.