+ 1

Why do we use void/int to declare a function?

why not always use int to declare a function/method?

16th Dec 2017, 12:06 AM
Ahmad Al Batsh
Ahmad Al Batsh - avatar
4 Réponses
+ 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.
16th Dec 2017, 12:16 AM
Dima Makieiev
Dima Makieiev - avatar
+ 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.
16th Dec 2017, 6:27 AM
ABDUL MANAN
ABDUL MANAN - avatar
+ 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.
16th Dec 2017, 7:21 AM
Dipti Muni
Dipti Muni - avatar