+ 9
Why use void?
In c++, there is a data type called void. It says that when you use it in a function, it will make the function in a valueless state. So why use it if it just makes the function in a valueless state?
5 Antworten
+ 6
This has to do with the type of the value that the function returns. Take an example: if you define a function, say "duplicate", that takes an int and returns an int, you declare it like this:
int duplicate(int x) {
return 2*x;
}
where the first "int" tells the compiler that the function returns an int value.
On the other hand, if a function does not return any value, for instance if it only prints something, you declare it as void:
void writeout(int x) {
cout << x;
}
This function doesn't return anything (there is no "return" command), thus the function is declared as void.
Hope this helps.
+ 4
What if that function is called only to cout?
+ 2
"void" uses if there isn't return value from this function .. such as a function to print a text ..
+ 1
use void in function Like Void func() when the function dont have a type
0
Void means empty.
It is used in a C++ program in order to return no value.