Very surprisingly, a fellow worker has compiled and uploaded the following sketch which does not contain the void loop(){} function. The uploaded program has worked well. Is this kind of program/sketch acceptable? Is void loop(){} a mandatory component of an Arduino sketch?
The hidden main() function calls loop() but the sample program never exits the setup() function so the fact that loop() is missing causes no problem
Here is the hidden main() function
/*
main.cpp - Main loop for Arduino sketches
Copyright (c) 2005-2013 Arduino Team. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }
void setupUSB() __attribute__((weak));
void setupUSB() { }
int main(void)
{
init();
initVariant();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
In fact, if you write your own main() function in the IDE then even setup() is not required
johnwasser:
If you make the while loop less obviously non-terminating, you get the usual "undefined reference to `loop'" error in the linker.
The modifier volatile of the declaration 'volatile bool forever = true;' makes the above error which gets away when void loop(){} is declared or the modifier is removed.
It would be interesting to know how the modifier 'volatile' makes the compiler to estimate that the while loop probably is not a terminating loop.
Because volatile tells the compiler, that the content of the variable might change unpredictably. The optimizer can not assume a value to be stored in the variable.
LightuC:
Because volatile tells the compiler, that the content of the variable might change unpredictably. The optimizer can not assume a value to be stored in the variable.
Very much convincing!
Is there any direct answer to my original question (Is void loop(){} a mandatory component of an Arduino sketch?) or I have to make my own answer based on the opinions of the posters?
GolamMostafa:
Is there any direct answer to my original question (Is void loop(){} a mandatory component of an Arduino sketch?) or I have to make my own answer based on the opinions of the posters?
The direct answer was already given, so I'm going to sum it up: No, the loop-function is not mandatory for an arduino sketch, if you define your own main function. If you don't you need to rely on the optimizer to optimize the call to loop away. This will almost certainly work as well, but you need to make it obvious to the optimizer, that you never exit setup.
Obviously not. loop() is not required. But in many circumstances an empty loop() function is required to stop the compiler complaining.
It's like if you own a car, do you have to use the car to drive to the shop? No, but it would actually be pretty difficult to never drive to a shopping establishment during the life of a typical car.
No, the loop-function is not mandatory for an arduino sketch, if you define your own main function.
I'd claim that if you write your own main function, you no longer have "an Arduino Sketch", you just have a C++ program that you're compiling with the Arduino IDE...
westfw:
I'd claim that if you write your own main function, you no longer have "an Arduino Sketch", you just have a C++ program that you're compiling with the Arduino IDE...
The above quote goes very much in line with the following spirit:
In the very first class of the Arduino Learning Course, it has been offered that the Arduino Sketch is composed of four components:
(1) Global Space (unmarked space at the top of setup() function)
(2) setup() function space
(3) loop() function space
(4) user() function space (unmarked space at the bottom of loop() function).
Because, global space and user() function space are not seen at the beginning classes, the learners are exposed only to setup() and loop() functions; they (the learners) started to believe that the Arduino Sketch has at least two components -- setup() and loop(). There is nothing such as main() function to the learners as they do not see it in the sketch.
Suddenly, a learner discovered a program/sketch which could be uploaded and executed without having a loop() function in the sketch. Under this circumstance, the Tutor has to offer satisfactory answer to the learner against his question -- Is void loop(){} a Mandatory Component of an Arduino Sketch?
GolamMostafa:
In the very first class of the Arduino Learning Course...
"The" or "some random as yet un-named" Arduino Learning Course? edit: by which I actually mean, surely there is more than one so not "the" but "a" course. Sorry, may have come across a bit terse there.
GolamMostafa:
Is void loop(){} a Mandatory Component of an Arduino Sketch?
Asked and answered even in your own opening post surely, since even without editing the hidden main() to do away with loop(), the code you as OP put forward compiled (and presumably ran). So answer has to be "no".