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? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

1st Dec 2016, 7:11 PM
Black Temple
Black Temple - avatar
10 Answers
+ 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!
1st Dec 2016, 8:42 PM
Manpreet Singh
Manpreet Singh - avatar
+ 7
@Designing heyy
2nd Dec 2016, 8:36 AM
Devendra Sanghavi
Devendra Sanghavi - avatar
+ 5
Manpreet awesome explanation. thank you
1st Dec 2016, 11:33 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
+ 2
int main () { . . . } return 0 TO ALLOW THAT LOOP TO BE RESTARTED AGAIN FOR A NEW USER ....
1st Dec 2016, 8:09 PM
Az.
Az. - avatar
+ 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
1st Dec 2016, 8:28 PM
Goutam Rudra
Goutam Rudra - avatar
0
how is void different here in this case? the void loop doesn't gets restarted?
1st Dec 2016, 8:11 PM
Black Temple
Black Temple - avatar
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.
1st Dec 2016, 8:22 PM
Mohammed Maaz
Mohammed Maaz - avatar
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?
1st Dec 2016, 8:26 PM
Black Temple
Black Temple - avatar
0
yeah. it did. thank-you mr manpreet for your efforts. very good example.
1st Dec 2016, 8:46 PM
Black Temple
Black Temple - avatar
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.
1st Dec 2016, 11:11 PM
Elio
Elio - avatar