Go Down

Topic: Recursion (Read 728 times) previous topic - next topic

Delta_G

I'm still getting used to the limited memory thing.  Is a recursive call like this going to give me problems?  It never gets called with _digits > 4.  Would it cause potential problems with a deeper stack of recursions? 


Code: [Select]
template<class T>
void padNumber(const T& _var, int _digits)
{
  T _place = _var % 10;
  if (_digits > 1)
  {
    padNumber( (_var / 10), (_digits - 1) );
  }
  LCD.print(_place);
  return;
}

Nick Gammon

I'm guessing each time you recurse you will use 6 to 8 bytes or so. So four times is OK.

Quote
Would it cause potential problems with a deeper stack of recursions? 


Depending on what else you have in your program you can work that out. Probably.

I had a maze generator that recursed. It only got up to about a 5 x 5 maze. Changing to a non-recursing algorithm I got a much larger maze out of it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PeterH

Sometimes recursion is the right answer, but printing a number in decimal hardly seems like a good reason.

Can't you just format the number to a string and then tell the LCD to display the string? Or if you really need to do it character by character, use a loop (iteration) instead of recursion.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up