Recursive Function Calls

Are they allowed?

  if(debugmode)
    Serial.begin(9600); // report to PC in Debug Mode
  Serial1.begin(57600); // To send updates to Topbox Unit
void updateScore(int p) // 0-3 -> Player 1-4, 4 -> All Players
{
  if(p>3)
    for(int i=0; i<4; i++)
      updateScore(i);
  sprintf(s, "@%d%06d", p, Score[p]);
  if(debugmode) Serial.print(s);
  Serial1.print(s);
}

Are they allowed?

Yes.

(Be careful)

Yes, I will. Oops, already discovered a mistake.

void updateScore(int p) // 0-3 -> Player 1-4, 4 -> All Players
{
  if(p>3)
    for(int i=0; i<4; i++)
      updateScore(i);
  else
  {
    sprintf(s, "@%d%06d", p, Score[p]);
    if(debugmode) Serial.print(s);
    Serial1.print(s);
  }
}

That's better, I hope.

Why do you want recursion here?

Just to save a few LOC's.

Stack space is much more valuable than a few lines of code!

Recursion is not the same as looping. You want to save code by looping, not be calling the function from itself.

Stack space is much more valuable than a few lines of code!

So, would you rather recommend I add another function?

void updateAllScores(void)
{
  for(int i=0; i<4; i++)
    updateScore(i);
}

In my experience, problems that are best solved by recursion are extremely rare. If you think you may need to use recursion, my advice is not to unless you really have to, and even then don't.

Recursive algorithms might occur more often than expected (not in embedded due to stack/mem constraints).
Best known recursive algorithm is quick sort, but they are also used in parsers etc.

That said all recursive algorithms can be transformed to a (often faster) loop, although complexity might grow.

Still, recursive algorithms have a natural elegance which reflects the recurrent nature of a problem(description).

SimLego:
Yes, I will. Oops, already discovered a mistake.

void updateScore(int p) // 0-3 -> Player 1-4, 4 -> All Players

{
  if(p>3)
    for(int i=0; i<4; i++)
      updateScore(i);
  else
  {
    sprintf(s, "@%d%06d", p, Score[p]);
    if(debugmode) Serial.print(s);
    Serial1.print(s);
  }
}



That's better, I hope.

Since "s" isn't local, I would be wary of using recursion here. Plus in this particular instance it doesn't seem warranted.

this is your requirement: // 0-3 -> Player 1-4, 4 -> All Players

The following code can be verified with above requirements easily

void updateScore(int p) 
{
  if (p >= 4) updateAllPlayers();
  else if (p >= 0) updatePlayer(p);
  // else ; // ignore negative values.
}

void updateAllPlayers()
{
  for (int p=0; p<4; p++) 
  {
    updatePlayer(p);
  }
}

void updatePlayer(int p)
{
  sprintf(s, "@%d%06d", p, Score[p]);
  if (debugmode) Serial.print(s);
  Serial1.print(s);
}

If you want to do it recursively (for academic reasons) think this way,

  • note that there is no loop...;
  • note two variations
void updateAllPlayers(int p)  
{
  p = max(p, 3);
  if (p >= 0) 
  {
    updatePlayer(p);
    updateAllPlayer(p-1);
  }
}

or 

void updateAllPlayers(int p)  
{
  p = max(p, 3);
  if (p >= 0) 
  {
    updateAllPlayer(p-1);
    updatePlayer(p);
  }
}