+ 4

Void doesn't return a value for a function. Then where does the need of such functions come that doesn't return a value?

18th Oct 2016, 2:05 PM
Jaya
7 Respostas
+ 13
For example if you want to print something on the screen you don't need that method to return something. Or if you want to add an element to a list with the method, you don't need anything to be returned either.
18th Oct 2016, 2:11 PM
Remmae
Remmae - avatar
+ 5
Yeap You will have to either change void to int or whatever, or to remove "return"
18th Oct 2016, 2:24 PM
Remmae
Remmae - avatar
+ 3
yes when it's void don't return anything
18th Oct 2016, 3:38 PM
Aaron Sarkissian
Aaron Sarkissian - avatar
+ 1
Ok and if we try to return a value in such case, will that give error?
18th Oct 2016, 2:23 PM
Jaya
+ 1
Got it! Thanks :)
18th Oct 2016, 2:30 PM
Jaya
+ 1
not 100% certain but I think that printing an integer to the screen in a void function might lead to an error. However, in my code, I was able to print an integer. My example might e different than what you are referring to. #include <iostream> using namespace std; void hope (int y) { cout<< y; } int main () { hope(89); return 0; } Jaya, I appreciate your question. It's also confused me.
27th Jan 2017, 7:15 PM
Mich
Mich - avatar
0
Yes, it will give an error. "void" simply means "that doesn't return any value". If you return any value in void function then it will definitely give an error
29th Jan 2017, 12:21 PM
Mohit Sood
Mohit Sood - avatar