With my experience in C++ programming, all functions need to be called in main() to get executed. However, there's no main() in above code and how Arduino call them?
Moreover, why do we need a loop() function? Cannot we just write a for-loop?
if you want to see what the main() (that gets added automatically and which calls setup() and loop()) looks like, it's right there on GitHub (if you can't find it on your own computer)
int main(void)
{
init();
initVariant();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
I agree that setup() and loop() is the logical way to structure most programs, but if you don't like it you can always define your own main() in the sketch, where it will override the definition in the core library. The only limitation I found in trying to reproduce the stock main() in my sketch was the call to initVariant() breaks it (multiple definition of `main'). That function is only used on certain boards (Gemma is the only one in Arduino AVR Boards) so it wasn't necessarily a deal breaker but it was unfortunate to not be able to reproduce all the functionality in the sketch. There may be a way around that, I didn't spend a lot of time on it since I didn't have any real need to do this.