[previous] 1520 [next] Functions are indirectly recursive.
CMA maintenance checks

A group of functions have been identified which appears to form a recursive sequence.

In general most recursive functions can be replaced with a stack and loop and in most cases the performance and scalability of the code will be improved.

Amongst others, recursive functions have two main drawbacks:

When asked to write a program which calculates the factorial of a number, it is common enough to see that a recursive function is used. Although a good example of what a recursive function is, this is a very bad example of when to use a recursive function.

Example:

int recursiveFactorial(int i)
{
  if (1 == i)
  {
    return i;
  }
  else
  {
    return i * recursiveFactorial (i - 1);
  }
}

int loopFactorial(int i)
{
  int result = 1;
  for (; i > 1; --i)
  {
    result *= i;
  }
  return result;
}

Even in these small examples above, for large values of 'i', the recursive implementation can take up to twice as long as the non recursive version.


MISRA-C:2004 Rules applicable to message 1520:

Rule  16.2  (Required) Functions shall not call themselves, either directly or indirectly.


QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents