+ 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?
7 Respuestas
+ 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.
+ 5
Yeap
You will have to either change void to int or whatever, or to remove "return"
+ 3
yes when it's void don't return anything
+ 1
Ok and if we try to return a value in such case, will that give error?
+ 1
Got it! Thanks :)
+ 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.
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