"return" function not working "void loop()"

Hi Guys,

I might be made to sound like a fool but I think there is an error using the "return" statement in the "void loop()". Although I'm only a learner in the C language, I believe the loop should stop running when the program comes to the "return" line as suggested in one of the "return" examples in the following link http://arduino.cc/en/Reference/Return

Can some of you guys please run the following code and see if the text keeps being printed to the serial monitor? It will only take two seconds of your time - I promise :)

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  Serial.println("Return should stop loop after this line!");
  return;
}

If somebody can give me any feedback one this issue, that would be much appreciated. If I'm doing/saying something stupid, don't be afraid to say so...

Regards

Adam

I believe the loop should stop running when the program comes to the "return" line

It does. Then, it gets called again. That's why loop() is called what it is.

Where do you expect it to return to and why do you think that after returning to where it was called that it will not be called again? There is a hidden function that calls loop() repeatedly, hence its name.

If you really want code to be run once then put it in the setup() function. If you want code to execute a number of times or in response to user or sensor input then stop, put it in the loop() function and set a flag to stop it being executed again, or create a while loop that will always be true.

Hi Guys,

Thanks for your feedback.

I was only intending to use the return statement as a "debugging" tool. Using while loops for debugging can become confusing after a while - well for me anyway :0. Bob, can you please elaborate a little bit more in regards to setting flags. Perhaps you can paste a small example code of what you mean by this?

So do I assume the example (second one) given on the Arduino reference web page is incorrect? The link is on my previous post and I have quoted it below. This was the source of my thinking!

void loop(){

// brilliant code idea to test here

return;

// the rest of a dysfunctional sketch here // this code will never be executed }

Cheers

Adam

If you want the sketch to stop in its tracks use exit:

void loop(){

// brilliant code idea to test here

exit (1);

// the rest of a dysfunctional sketch here
// this code will never be executed
}

So do I assume the example (second one) given on the Arduino reference web page is incorrect?

It looks OK to me.
Anything after the “return” will never be executed.

(NB “return” is not a function)

Hi Nick,

That seems to work but I can't see "exit" under Arduino's list of Control Structures given in the following link http://arduino.cc/en/Reference/HomePage Where did you get that one from and why do you need to put (1) after exit? I did notice that this statement effected the printing of the line to serial monitor. Only prints "Re" now. Would you agree what Arduino has on their web page is incorrect?

Hi AWOL,

Did you actually compile the code and give it run on a board? It looked good for me as well but you can see what the other guys have said about it. Regarding the NB; your right, it is a Control Statement. I do need to watch my programming speak being a Mech Eng.

Cheers

Adam

I can't see "exit" under Arduino's list of Control Structures given in the following link http://arduino.cc/en/Reference/HomePage

Because the home page only lists functions/feature that are unique to the Arduino. exit() is a standard C function, so it, along with many other C functions can be used on the Arduino.

why do you need to put (1) after exit?

The parens are because you are calling a function. The function expects a value.

I did notice that this statement effected the printing of the line to serial monitor. Only prints "Re" now.

The rest of the letters will be shifted out when exit() ends. Never, that is. If you need to see all the text, that is just about the only place where Serial.flush() has a place in your code.

Hi PaulS,

Thanks for that. Makes sense now and very helpful as I always thought Arduino only took bits and pieces out of language.

Do you know if the Arduino.h pulls in all the standard C libraries or do we need to specify these at the start still?

Regards

Adam

Do you know if the Arduino.h pulls in all the standard C libraries or do we need to specify these at the start still?

Personally, I don't worry about until the compiler complains about an undefined symbol. Then, I include the needed header file.

Did you actually compile the code and give it run on a board?

I hadn't when I wrote it, but now I have. It works exactly as I expect it to - no Spanish Inquisition

void setup ()
{
  Serial.begin (9600);  
}

void loop ()
{
  Serial.println ("I didn't expect the Spanish Inquisition");
  return;
  Serial.println ("No-one expects the Spanish Inquisition"); 
}

Hi AWOL, thank you thank you thank you...

I understand now. I was head-butting the screen for ages over that one. It hits return and then starts the loop again... it really it brilliant.

Cheers

Adam

I consider the thread now closed. Thanks for everyone's who input.

Individually, I will not bother about before compiler complains in relation to a undefined mark. After that, I are the necessary header file.

Individually, I will not bother about before compiler complains in relation to a undefined mark. After that, I are the necessary header file.

And who could argue with that?

Bob, can you please elaborate a little bit more in regards to setting flags.

Things have moved on in this thread whilst I have been away from the PC. What I had in mind was to initialise a boolean variable to false then change it to true when the code to be run once had executed the first time, and to check before running the 'once only' code that the variable was not true, thus preventing it from running again.

adamatcooranbong: I did notice that this statement effected the printing of the line to serial monitor. Only prints "Re" now.

exit turns off interrupts. That's why. You could do:

Serial.flush ();
exit (1);

However wanting your program to stop dead like that is a little unusual.

Would you agree what Arduino has on their web page is incorrect?

No, I don't. "return" returns you to the calling function (which happens to be "main") as advertised. And since loop, well, loops, it is called again.

An alternative would be:

if (some_sort_of_error)
  while (true) { }

That loops indefinitely without turning off interrupts.

loop() is declared as void in which case it cannot return anything. break is a quick way out.

Mark

holmes4: loop() is declared as void in which case it cannot return anything. break is a quick way out.

You seem to be confusing not being able to return a value, with not being able to return. You also seem to be confusing the requirement to return from the loop() function with being able to break out of a control loop.

It's understandable, though; the people who named the loop() function made this sort of confusion more or less inevitable when they chose such a daft name for it.

holmes4:
loop() is declared as void in which case it cannot return anything. break is a quick way out.

No, it isn’t.

void setup ()
  {
  }  // end of setup

void loop ()
  {
  // whatever

  break;  
  }  // end of loop

Gives:

sketch_jun26b.ino: In function 'void loop()':
sketch_jun26b:10: error: break statement not within loop or switch

Of course it’s within “loop” but not within “a loop”.

I guess it would clarify things if you knew what's going on in Arduino main(). I believe it's something like this:

void main()
{
  setup();
  while(true)
    loop();
}

So, if you return from loop(), it'll just get called instantly right after by the while loop.