void vs int(c language) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

void vs int(c language)

what is the difference b/w void and int. if we use int and give return function in it, what actually it does?

26th Sep 2019, 6:34 AM
Reiner
Reiner - avatar
4 Answers
+ 4
The advantage of int functions is that they can return a int value and can be treated like an int variable. void functions however cannot return any value. An example of an int function: int multiply(int a) { return a * 2; } int main() { int test = multiply(2); // test == 4 return 0; }
26th Sep 2019, 6:55 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 4
reiner A The compiler will automatically put it there for you. But if you forget to return something in a normal int function it will result in a compilation error or at least a warning.
26th Sep 2019, 7:01 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 2
since you didn't mention a language i'll do examples in Java, but they're probably quite similar A void-function/method does not return anything. it just executes the commands inside the curly-brackets {}: public void sayHello(){ System.out.println("hello"); } A funtion/method which has int/float/... (must) returns a int/float/... and before returning it executes all code before it hits the return-statment. Opon hitting the return-statment, the function/method is done: public int getOne(){ return 1; }
26th Sep 2019, 6:56 AM
Anton Böhler
Anton Böhler - avatar
0
what will happen if we remove return 0 in main function
26th Sep 2019, 6:59 AM
Reiner
Reiner - avatar