Surprisingly the Arduino accepted it; however, it behaves oddly. Rather than receiving on the serial monitor something like:
Hello
I'm after setup
Hello
I'm after setup
Hello
I'm after setup
...
I get
Hello
I'm after setup
I'm after setup
I'm after setup
...
It's as if the Arduino is deliberately ignoring the call to setup. I've tried searching the forums and reference with little success, though I may have missed something. My question is four fold:
1) What exactly is the Arduino doing as it executes this code? 2) Why is it doing that? 3) Is the behavior of this code well defined or is it implementation dependent? 4) If the behavior is well defined, where is it specified?
I figure that if the compiler doesn't complain too hard, "isn't needed" is sufficient.
If it isn't "supposed" to be there, gcc would bitch about it, big time.
sketch_sep22a:0: error: ISO C++ forbids declaration of 'setup' with no type
sketch_sep22a.cpp: In function 'int setup()':
sketch_sep22a:0: error: new declaration 'int setup()'
F:\Applications\Arduino\arduino-1.0.1\hardware\arduino\cores\arduino/Arduino.h:117: error: ambiguates old declaration 'void setup()'
I get this error:
> sketch_sep22a:0: error: ISO C++ forbids declaration of 'setup' with no type
> sketch_sep22a.cpp: In function 'int setup()':
> sketch_sep22a:0: error: new declaration 'int setup()'
> F:\Applications\Arduino\arduino-1.0.1\hardware\arduino\cores\arduino/Arduino.h:117: error: ambiguates old declaration 'void setup()'
Because you're missing a "void" at the beginning...
It doesn't really make sense to print "I'm after setup" - you will see that many times. The Arduino core first calls setup once, and then loop repeatedly. So loop is "after setup" but it is called multiple times.
I get this error:
> sketch_sep22a:0: error: ISO C++ forbids declaration of 'setup' with no type
> sketch_sep22a.cpp: In function 'int setup()':
> sketch_sep22a:0: error: new declaration 'int setup()'
> F:\Applications\Arduino\arduino-1.0.1\hardware\arduino\cores\arduino/Arduino.h:117: error: ambiguates old declaration 'void setup()'
Because you're missing a "void" at the beginning...
Is that windows 8?
lol, I didn't notice "void setup()" inside of "void loop()" lol
I want to thank you guys for time you've taken to post here, it has been very helpful so far. There's been a small amount of confusion on a point which I'm sorry for failing to clarify. I'm not calling setup inside loop to actually accomplish anything, rather I'm trying to mess with the Arduino in order to understand it better. If I throw a wrench into an engine and explain why the engine broke the way it did, then I think I will be closer to understanding the engine itself.
With that in mind, I'd like to paraphrase what I've learned to make sure I've got things straight. The Arduino will blissfully call setup inside loop repeatedly just like any other function, though telling it to do so is not advisable. However, I get the behavior I see because Arduino reads void setup (); as a 'function prototype' instead of a function call which is typed as, setup();
Function prototypes act as variable declaration for functions. In effect loop says, "Hey Arduino, there's this function called setup which doesn't return anything, just so you know." Loop then prints a message to terminal and repeats.
Yes, sort of. However the function prototype doesn't "belong" to loop. It's confusing and misleading to have it there. Normally you put them near the start of the program.
I'm not calling setup inside loop to actually accomplish anything, rather I'm trying to mess with the Arduino in order to understand it better.
To better understand the Arduino internals I suggest you also take a look at the "system" source code, too. A good starting point is hardware/arduino/cores/arduino/main.cpp
mdimiou:
1st)If i call setup() inside loop() i will reset Arduino one more time with my decision?
No, the Arduino will not reset with calling setup inside loop. If you have a void setup() at the beginning of your code and another one in void loop(), it will likely cause bugs in the output similar to the OP.
2nd) When i am opening a new Serial Port fo a connection between my Arduino and my pc Arduino is going to reset???
For example..... i am opening and closing two times a Serial Port..... Arduino will reset two times?
If you are opening and closing the Arduino IDE's serial monitor, yes it will reset each time.
is perfectly valid, it tells the compiler to ignore any return value from this function. It is not necessary in this case, but shouldn't hurt. It is usually used in code like this:
(void)sprintf(...);
where you are telling the compiler that you are throwing away sprintf()'s return value on purpose. Some code checking programs like lint will complain if you throw away a return value silently.