+ 1
As we know void doesn't returns any value to compiler. What is the meaning of 'return value to a compiler'? Why is it important?
10 Réponses
+ 8
@Designing has already mentioned. But anyway I'll write as how i understand the process.
Image compiler as a manager of a factory(code) and functions are workers who work for the factory. Now, manager(compiler) asks workers to do some factory job(code calls function). Worker start doing his designated job(code inside that function runs). During that time manager wait for that worker to finish his job so that after job completion he can give new job to next worker. Now, the thing is manager is sitting in his office talking to workers on mobile. He can't see them. So, he will wait for worker to say that the job is done, otherwise manager will keep on waiting. Workers(function) are supposed to let manager(compiler) know thag their work(code inside function) is done and manager can assign next job(run next line after function call). This keep going until manager completes all jobs(all code is run inside main()) and he close the factory(return 0 at the end of main()).
Hope it makes sense.
Phew, i put a lot of thought in it!
+ 7
@Designing heyy
+ 5
Manpreet awesome explanation. thank you
+ 2
int main ()
{
.
.
.
}
return 0
TO ALLOW THAT LOOP TO BE RESTARTED AGAIN FOR A NEW USER ....
+ 2
returning a value at the end of a function or at the end of the main function is a great way to keep tab on errors in program execution and also use the return code to pipeline multiple things programmatically.
void says no return to be expected. and it is not just to compile. it also happens when you run it.
thanks
0
how is void different here in this case? the void loop doesn't gets restarted?
0
Az. ad firstly you wrote return 0 after the main() function which is incorrect. Secondly return 0; is not for restarting the loop unless you call main() through it recursively just like return main().
Coming back to the question, when you use int main() then the compiler is expecting the main() function to return a value. Therefore it's good to return a value by yourself otherwise the compiler returns a random value by itself, and may also produce a warning message. However if you are using void main() then there is no need for the return statement.
0
okay it makes sense now :)
so if i don't return the value, calling the class in main will give me an empty class ? is that what you mean?
0
yeah. it did.
thank-you mr manpreet for your efforts. very good example.
0
Functions do not return values to compilers they return values to the caller. when you use return in a void function is to terminate the function before it reaches the end.