Pages: [1]   Go Down
Author Topic: Recursion  (Read 640 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
God Member
*****
Karma: 17
Posts: 744
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18722
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: