ALWAYS follow the same rules for executing function calls when reading recursive code, as they are exactly the same for a recursive function.
Mathematical Definition:
$$ n! = \begin{cases} 1 &\text{if } n = 0 \\ n*(n-1)! &\text{if } n > 0 \end{cases} $$
C Code for Factorial:
int factorial(int n) {
if (n <= 0) {
return 1;
}
return n * factorial(n-1);
}
factorial
that takes an int
and returns an int
.factorial(n-1)
.factorial(n-1)
.factorial(n-1)
is multiplied by n
and returned.ALWAYS remember that recursive functions follow the same execution rules as non-recursive functions. Understanding these rules helps in reading and tracing recursive code effectively.
Which one of the following best describes recursion
1 point