+ 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?

21st Jan 2017, 4:50 AM
UnidentifiedProgrammer
UnidentifiedProgrammer - avatar
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.
21st Jan 2017, 5:11 AM
Álvaro
+ 4
What if that function is called only to cout?
21st Jan 2017, 5:06 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
"void" uses if there isn't return value from this function .. such as a function to print a text ..
23rd Feb 2017, 3:45 PM
Mr.saedsadi
+ 1
use void in function Like Void func() when the function dont have a type
25th Feb 2017, 4:44 PM
Hamzaoui Med Nour
Hamzaoui Med Nour - avatar
0
Void means empty. It is used in a C++ program in order to return no value.
1st Mar 2017, 4:06 PM
Neha
Neha - avatar