break EVERYTHING what is Arduino doing, but not reset

Hi,
I want’s to break all operations, like if in fuction4 happends something, thatsays me “stop everything, break all operations, start again”, I wants to break function4, but I can’t use return, becasuse function3 is waiting when function4 will be done and then do something, function 2 is waiting on function3… But DO NOT reset variables and DO NOT execute setup()

Maybe better to understand in attached picture.

Is it okay to call

if(something)
{
    loop(); //stop everything and go back

or is there any better way, which I should use
Thanks.
(sorry for my english)

Untitled.png

Is it okay to call

No, it is not. To end a function, even loop(), simply return;.

PaulS:
No, it is not. To end a function, even loop(), simply return;.

return ends function4, which is called inside in function3, so the function3 will continue and finish, but I want to stop everything and function3 can’t finish

return ends function4, which is called inside in function3, so the function3 will continue and finish, but I want to stop everything and function3 can't finish

Your psuedo-code does NOT call functions. It has blocks of code. Return in a block will end the function.

Going back to the start of loop() because some condition occurs n function calls deep does not make sense. It's time to re-architect your code if you think it is appropriate to jump somewhere else when a problem occurs.

PaulS:
Your psuedo-code does NOT call functions.

In this case I suspect the OP intended it to represent a series of calls to functions.

If loop() calls functionA() and functionA calls functionB() --- how do you get from functionB all the way back to loop()?

I think the only way to do this is to write code in functionA() that will cause it to finish (and return) if it receives a particular return value from functionB.

...R

I think the only way to do this is to write code in functionA() that will cause it to finish (and return) if it receives a particular return value from functionB.

+1

And, now that I have some reassurance that I was on the correct track ...

I think that it may be appropriate to re-consider the structure of the program to try to avoid the need for a return back through several levels of functions. For example maybe the function that is likely to require the process to abort could be called directly from loop().

...R

I think that it may be appropriate to re-consider the structure of the program to try to avoid the need for a return back through several levels of functions.

I'll disagree with that. Every function should be written so that it properly deals with a failed return code from any function that it calls.

The whole idea behind a function is that it encapsulates some bit of functionality that is known to work. Then, the function can be called anytime that functionality is needed. Restricting the use of that function to one or two levels deep is not the way to go.

I don't disagree that a complete restructure of the code is possibly needed. We haven't seen what OP is doing. OP asked a generic question to which the answer was no, you can't do that.

As to how to do what OP really wants to do, that depends on how OP is doing it, whatever it is, now.

The most obvious method would be to have a global variable (perhaps called abort). When conditions dictate that this has to happen, it should set abort to be true and return.

In the meantime, any function you write should pay it's respects to this variable in it's logic. The functions could also do their own cleaning up before returning. (such as resetting any global variables or turning off any time critical outputs etc..)

PaulS:
I'll disagree with that. Every function should be written so that it properly deals with a failed return code from any function that it calls.

I agree with this, and it was not the point I was trying to make.

I suspect that a project that calls down through several levels of functions and then suddenly has to abort all the way back to the top level could be better designed - perhaps even by doing the "abort" test first.

...R