0

Why we are not using void main for declaring a function? Instead we are using such as int?

19th Oct 2016, 2:53 PM
Abhay Selva
Abhay Selva - avatar
3 odpowiedzi
+ 5
thx that was really useful information!
21st Oct 2016, 8:08 AM
F.Heeh. 2nd Yr. Hebron University‏‎
F.Heeh. 2nd Yr. Hebron University‏‎ - avatar
+ 3
Main always returns a value. Even if you don't specifically declare it to return a value, most compilers will pretty much assume you meant to put "return 0;" at the end of your main. We use an integer return type for main so that we are able to diagnose whether our program ran successfuly, or if it ended in a way we didn't expect, in which case we return a value other than 0 (usually 1). Helpful in larger programs with diagnosing problems. Always have the return type for main as int; it is the C++ standard, and many compilers may not even let you put any other return type.
19th Oct 2016, 3:07 PM
Cohen Creber
Cohen Creber - avatar
+ 1
any function can return a value and main also. but the usage is different. you may want to use main return value when you call it within a process, like a BATCH(.bat) file in windows or shell file in *nix. assume your program as a function call in a batch file and getting the return value of you program through main in batch file, so you could understand if your program exited (returned) with the conditions you expected.
19th Oct 2016, 9:58 PM
TwinChrist