Non-blocking functions / is recursion a good idea on arduino?

I am trying to combine two logics that work separately, but when doing so I see that actually I am preventing loop() to be executed.

In the code I am creating my own loop with a self-calling function, that keeps self-calling until a condition is fulfilled, then it calls a 2nd function that eventually calls again the 1st function in an eternal loop. But this makes loop() never being called.

How would you restructure the code? My assumption is that I could rewrite the logic to be executed within the loop(), but maybe there is a way to execute rotation() in a non-blocking manner?

void setup()
{
  rotation();
}

void loop()
{
// never executed, but I want to execute the code that is here
}

void rotation()
{
  if (condition)
  {
    rotation();
    return;
  }
}

This is a recursive call, but it is incomplete.
To do proper recursion, you need a base case and at least one recursive call.
This example is missing the base case, wherein something is returned without calling the function again.

Recursion can eat your memory right up. With these tiny processors it is advisable to use a while loop instead of recursion, when possible.

Thanks, do you have any link to read more about recursion vs loop in arduino?

Not particularly for Arduino.
Explain more about what you'd like to do and perhaps I, or others, can offer better advice.
I see nothing that changes condition

Depending on how often you call your function rotation. You could even occupy Gigabytes of RAM in a HIGHEND Computer making this highend-computer crash.

The code that you have posted has only a single "logic"

the function-call rotation().

There is no second "logic"

It is not yet clear what you want to do. You should describe the functionality in normal words.

I emphasise normal words. You are - not yet - a coding expert. But you are an expert about your project and about normal words.
This means your functional description (in normal words) will be professional.

So please give a detailed but normal worded description of your functionality
Strictly avoid programming-terms. Just normal words.

best regards Stefan

Please explain what you really want to do.

Discussing the proposed solution is a waste of time.

this seems pointless. i don't see where "condition" can ever change value



Don't take it so literally. You might as well have pointed out that the function does nothing but call itself, even if it did have a way to bottom out and unwind.

Or asked where condition is declared, for that matter. @kugerard was clearly just providing a structural outline of what the arrangement might look like.

So @kugerard, I am with everyone who just want to know more about what it is exactly this recursive function is going to accomplish.

There are ways around recursion. As has been mentioned variously, on a tiny machine it might be best to do.

Post the code for each of the things that want to be combined.

a7

Equivalent logic using tail recursion, as preferred:

void rotation() {
  if (!condition) {
    return;
  }
  rotation();
}

@kugerard yes. please explain what you're trying to do

As others have implied, show the code.

Yes, I was sort of asking a conceptual question, and @er_name_not_found @StefanL38 and yourself have provided the solution stating that using recursion is not a good idea on Arduino, regardless of whether using recursion was justified or not in my code (which I haven't shown, as @alto777 has described very well, btw thank you for the nice tone).

I had no idea that recursion was a bad choice, I think I used this to force a repaint in the browser in a nodejs app, so I guess here it just pop up naturally.

But with this information, I've just discarded recursion and moved it to the loop. I'll give it a try, and if I cannot manage I will post a new question with specific code. But I'd close this question because I think there is a satisfactory answer, which is, forget recursion unless you have very good reasons to stick with it, so thanks a lot to everyone.

doesn't it depend on the application? in some cases it may make the code much simpler

Yes, complex branching recursion is difficult to do with loops, but that was not demonstrated in OP's example.

Yes, say for making statistical counts on multiple possibility data sets, or multidimensional data analysis, but using a uC for such things is not typically done.

For most cases that would be performed on Arduino level equipment, avoiding recursion when possible is sound advice.

i thought the classic example of recursion was in Algorithms + Data Structures = Programs by Niklaus Wirth's where he introduced pointers and used recursion in tree structures

That can still be performed with looping and your tree can be maintained as a balance structure within an array.
I mean if you are absolutely determined to waste most of your RAM, you can do what you like, but does not make it good advice for beginners trying to learn uCs.

are you "absolutely" sure?

there are good and bad reasons for doing things. it's rarely correct to say something is "absolutely" a bad idea

it would be better to explain the reasons when something is not a good idea

Let's review my comments:

Explain where I said absolutely.

I am talking about best practice to avoid memory issues that can only be caught in a debugger while you are picking nits.

Bottom line, see the quoted comment at the beginning of this reply.

???

Using recursion often wastes RAM, unless it's properly formatted and we'll designed tail-recursion that the compiler can optimize down to a simple loop. I'd hope you'd be aware of that before arguing for recursion in a device with practically no RAM.