Exit void loop without return the next instructions

First time I apologise for my bad english.

I have a problem. I have void setup, void loop and 1 fuction more. Example

Void setup(){
Intruction1;
call();
}
void call(){
Intruction2;
}
Void loop(){
Instruction3;
If (x==Y){
-----------My problem
}
Instruction4;
}

I need a command that if x=y go to, for example, void call() fuction BUT I need when void call() end not return on last postion in void loop() fuction and execute instructuion4. I need run the code from void call() like the firt time the code started, these way when the void call() end void loop() starting from instruction3. I dont want restart the program obiously. I Supose it will be necesary break the void loop() or something like that.

Thanks so much

You can use the return statement to get out of the loop() function (it's not called a void but a function)

void loop()
{
  if(a == b)
  {
    return;
  }

  c = a;
}

Functions can be declared to return a value or not to return a value.

void loop()
{
}

int square(int a, int b)
{
  return a*b;
}

loop() is a function that returns nothing, square() is a function that takes two integers as parameters and returns an integer.

Sounds to me like you just need to use an else so the last line only runs if the if statement is false.

Thanks sterretje

sterretje:
You can use the return statement to get out of the loop() function (it's not called a void but a function)

void loop()

{
  if(a == b)
  {
    return;
  }

c = a;
}




Functions can be declared to return a value or not to return a value.


void loop()
{
}

int square(int a, int b)
{
  return a*b;
}



loop() is a function that returns nothing, square() is a function that takes two integers as parameters and returns an integer.

Thaks so much. The key is return setup(); for example. This way you can return where you want

You cannot return "wherever you want", only where your function was called.

You can possibly use setjmp()/lngjmp(), but they are rarely needed.

KeithRB:
You cannot return "wherever you want", only where your function was called.

You can possibly use setjmp()/lngjmp(), but they are rarely needed.

Exactly, but you can prepare a fuction where you need start again. For example. I have it:

Int qdata(intx,int y){
If(x>=2){
return setup();--------//It not found becouse you havent a void fuction. You alredy have a return
}
return x*y;

}

You can do it

Int qdata(intx,int y){
If(x>=2){
rest();
return x*y;

}

void rest(){
return setup();
}

No you can't

return setup() will give you an error. You can return a function pointer - assuming you have the function declared correctly. And you can have as many returns in a function as you want.

Look my test code:

int k=0;

void setup() {

Serial.begin(9600);
Serial.println("IN SETUP");
delay(3000);
ini();

}
void ini() {
Serial.println("IN 1");
delay(3000);
}

void loop() {
Serial.print("-----------------"); Serial.println(k);
Serial.println("IN 2");
delay(3000);
k=k+1;

if (k>=3){
return setup();
}
Serial.println("IN 3");
delay(3000);

}

You can test this code and check for yourself that its ok

It is not ‘OK’

What it is doing is executing setup() and then returning from loop(). It is not “returning” to setup().

You could do the same thing like this:

if (k>=3) {
     setup();
     return;
}

If you don’t believe me, try to “return to” a function that returns a value.

KeithRB:
It is not ‘OK’

What it is doing is executing setup() and then returning from loop(). It is not “returning” to setup().

You could do the same thing like this:

if (k>=3) {

setup();
    return;
}




If you don't believe me, try to "return to" a function that returns a value.

if (k>=3) {
setup();
return;}

Its not found. but I think that you dont use arduino software for that reason my code is wrong for you.

Anyway, if you run my code in Arduino software (change k>=3 for k==3) The result you will obtain in Serial window is the next:

IN SETUP
IN 1
-----------------0
IN 2
IN 3
-----------------1
IN 2
IN 3
-----------------2
IN 2
IN SETUP
IN 1
-----------------3
IN 2
IN 3
-----------------4
IN 2
IN 3
-----------------5

Look at when k==3 go to setup() and write “IN SETUP” after call INI() an write “IN 1” and now not exist another call thus the software run void loop, BUT (IMPORTANT) THIS MAKES IT FROM THE BEGINNING. You can see as the first tense wirte is “IN 2” and no “IN3”. Very important detail which I was loking for.

the other hand, if you only use setup(); you are calling a fuction so when de fuction setup() and INI() end the code come back after if(k==3) and will write “IN3”

Variable 'k' is global - so it is effectively static.

Thus it is not cleared when setup runs.

No it does not.

when you call
return setup();

It will call setup();
Then it will return from setup().
Then it will return from loop() in the exact state it was. It does not restart the program and act as if main() is calling setup() and loop() the first time.

If you do it this way:

if (k==3) {
setup();
return();
}

you also will not call IN3.

return simply is not doing what you think it is doing - it can’t!

KeithRB:
No it does not.

Who are you responding to ?

wargadex