+ 3
What if we use void instead of int...?
will it effect output of the code...? why we need or don't need to return a value to the main function? kindly explain...
2 Respostas
+ 10
using void main the code wont compile at all when using a modern compiler.
when using int main we return a value of 0 to tell the operating system that the program executed correctly.
if the return statement is missing the compiler will automatically insert one for you
edit: http://www.stroustrup.com/bs_faq2.html#void-main
+ 6
if yor functions returns a value you can call it to directly retreive that value
int sum(x, y){
int addition = x + y;//loocal variable
return addition;//returned value
}
//print the return value
System.out.print(sum(3,8)); //will output 11
with a void function it would look like this:
int multiplication; //global variable
void mult(x, y){
//global variable changes its value
multiplication = x * y;
}
mult(2,6); //call function to change global variable
//print global variable
System.out.print(multiplication);//output 12