0

Should you write void main() or int main() in code?

Using main() is mandatory. But is it void main() or int main() ? please post "What you use in your program" as your answer from the options and Say WHY USE THAT? 1. void main() 2. int main() 3. Both can be used AND NO DIFFERENCE 4. Both can be used BUT THEY ARE DIFFERENT //////////please explain your answer......////////// if you dont know, no problem....just post your answer ("what you use in programs") with OPTION ONLY, dont post unnecessary stuff, or i will downvote.

15th Jul 2017, 7:03 PM
Food
Food - avatar
12 Answers
+ 1
Int main allows you to return 0 or something else meaning you can tell Windows that the process failed as apposed to closing normally. With void you don't have that manual control.
15th Jul 2017, 8:02 PM
josh mizzi
josh mizzi - avatar
+ 1
void means that it's not returning anything. int main() returns a int, I don't know what language you are using, so I will try to explain this as best as I could: For example if I have a code: "[insert a int that is named Number1] [insert a int that is named Number2]" Then I have in a start function or something "Number1 = main()" Which in int Main(int x) It says Int x because it is making a local variable called "x" Lets say in main it just says "x = 1" return x; Now that it says "x" is "1" then you return "1" Number1 = 1. With void, it doesn't return, so in void you would just do void main() { //code here }
15th Jul 2017, 7:36 PM
Dillion Piazza
Dillion  Piazza - avatar
+ 1
So, in c++ for example, you use int main (), java used void main (), that means in c++ there's a return in the main method while java does not have one.
15th Jul 2017, 7:42 PM
Jonas Schröter
Jonas Schröter - avatar
+ 1
In C++ your main function must always return int, anything else and you have invalid C++ code. Yes, void main is invalid even though alot of people here seem to think better... Maybe you believe the creator of C++ better than me so: http://www.stroustrup.com/bs_faq2.html#void-main So option 2 is the only correct option.
15th Jul 2017, 8:07 PM
Dennis
Dennis - avatar
+ 1
@Ceaseless Well, I kinda figured people would read the link for a better explanation :P
15th Jul 2017, 8:20 PM
Dennis
Dennis - avatar
0
@TOTALLYnotDILLION but you didn't explain which to use, and where? you just explained how functions work (main is also a function) so okay. my question was to have an answer to explain which main to use. Not how they work. Although, thanks for the explanation @TOTALLYnotDILLION
15th Jul 2017, 7:48 PM
Food
Food - avatar
0
Because what if you're not returning anything?
15th Jul 2017, 7:53 PM
Dillion Piazza
Dillion  Piazza - avatar
0
like return makes more work because you have another function but it's much more readable.
15th Jul 2017, 7:54 PM
Dillion Piazza
Dillion  Piazza - avatar
0
when you run a program this happens: specially if you use it in system with any application like games, music player, software, etc., i am not saying about IDEs and console applications. You use the application -- you see the frontend -- gui or cli you use it--- in the back it uses its resources, small programs, library files etc., And there wont be one program in those kind of softwares or system applications. So, when you use it, it calls every other simple programs that you wrote. For consistentcy in execution, every program should say "Hey I Am safe and did my part of work without any error" to whichever the program called it. so, on top your software runs on operating system, which is needed for interacting with hardware, so your program should tell OS an acknoewledgement, that it had done something and thats done perfectly, so returns a VALUE to the OS. then OS handles whatever it should. if you dont return a VALUE to OS , it wont know what happened, i mean the program will work and the effect will happen, but OS cant know, if the program ran nicely without any error or not. so, if you use retutn value , it'll tell 'I am good' and if anything wrong happens, it wont return integer or it will return -1 or some error code. If you dont use return, no message will be told to OS. Now to Java.... Java is inside JVM (Java Virtual Machine) so isolated from OS. no direct interaction with OS by the code you write. One of the reasons why Java is safe! Also, within java programs you can use 'Exception Handling' to handle exceptions , which is a very powerful feature of Java...so no need to explicitly tell if anything happened...Handle them with Exception Handlers.... Hope this helps!!!
15th Jul 2017, 8:09 PM
Food
Food - avatar
0
@Dennis thanks for answer, you are correct on void making error.But you didnt say why. See my answer for full explanation
15th Jul 2017, 8:15 PM
Food
Food - avatar
0
@josh mizzi Exactly said ! First Correct answer, Marked as Best answer { For Now } 😉
15th Jul 2017, 8:16 PM
Food
Food - avatar
0
@Dennis true, or for people who can't read well, to summarize it.
15th Jul 2017, 8:21 PM
Dillion Piazza
Dillion  Piazza - avatar