I've seen D code that uses void main
. Is this legal? If yes, is returning non-void
(int
) also legal? Why is this allowed in the language?
From the D Language Reference
main() Function
For console programs, main() serves as the entry point. It gets called after all the module initializers are run, and after any unittests are run. After it returns, all the module destructors are run. main() must be declared using one of the following forms:
void main() { ... }
void main(char[][] args) { ... }
int main() { ... }
int main(char[][] args) { ... }
So void main
is legal.
From the same docs, return statement part:
A return exits the current function and supplies its return value. Expression is required if the function specifies a return type that is not void. The Expression is implicitly converted to the function return type
So it appears that returning something from a void function is not explicitly disallowed (and indeed doing so compiles fine).
(Why would you want to do that though?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With