+ 1
Why do we use void/int to declare a function?
why not always use int to declare a function/method?
4 Answers
+ 2
It is the way to tell compiler what type of value this function will return, so when you will call this function and assign it result to some variable type checking will be performed. But it is need to be done only in strongly typed languages like java or c# you dont need to do this in javascript or php.
+ 1
It defines the return type i. e. the datatype of values you want in the output. For example, if we write int main() in C then it means that the data type of values obtained in the output will be int.
+ 1
It seems we use void to declare a function before calling it.
void print() {
cout << "Hi!";
}
int main() {
print();
return 0;
}
Then, we use int to execute the function.
In above example, we declared print() function that has a statement (VOID) and then we called the main function that had print() statement in it. ( INT).
Void doesn't print value unless the function print() is called upon in the int main(), which is to output value.
That is a good question and I hope this helps.