Setup called inside Loop- what is going on?

I tried compiling the following code with Arduino 1.0.1 into a Duemilanove (328) using AVRISP mkII;

void setup ()
{
  Serial.begin(9600);
  Serial.println("Hello");
}

void loop ()
{
  void setup ();
  Serial.println("I'm after setup");
}

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?

Thank you for your time

If you remove the blatent syntax error (which somehow compiles...) it just gets confused by the multiple Serial.begin()s

  void setup ();

Is not how you call a function

setup();

is how you call a function.

I don't see a syntax error, I see a function prototype where a function prototype isn't needed.

I don't see a syntax error, I see a function prototype where a function prototype isn't needed supposed to be.

I fixed that for you...

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.

The setup() only runs once when you turn on or reset the board.

Loop() runs over and over again non stop.

So:

Hello
I'm after setup
I'm after setup
I'm after setup

Is exactly what you told it to do.

If I try to compile this:

setup ()
{
  Serial.begin(9600);
  Serial.println("Hello");
}

void loop ()
{
  void setup ();
  Serial.println("I'm after 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()'

Why would you want to call setup() inside loop() anyway ?

codlink:
If I try to compile this:

setup ()

{
  Serial.begin(9600);
  Serial.println("Hello");
}

void loop ()
{
  void setup ();
  Serial.println("I'm after 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...

Is that windows 8?

This compiles.

void setup ()
{
  Serial.begin(9600);
  Serial.println("Hello");
}

void loop ()
{
  Serial.println("I'm after setup");
}

Compare carefully to what you had. You should not even be attempting to call setup from inside loop.

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.

WizenedEE:

codlink:
If I try to compile this:

setup ()

{
  Serial.begin(9600);
  Serial.println("Hello");
}

void loop ()
{
  void setup ();
  Serial.println("I'm after 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...

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

:slight_smile:

1st)If i call setup() inside loop() i will reset Arduino one more time with my decision?

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?

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.

One other minor point:
the syntax:

(void)setup();

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.