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?
4 odpowiedzi
+ 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;
}
+ 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.
+ 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;
}
0
what will happen if we remove return 0 in main function