A function that calls itself to solve a smaller version of the same problem.
| Type | Description | Processing Location |
|---|---|---|
| Head Recursion | Processing before recursive call | Above recursive call |
| Tail Recursion | Processing after recursive call | Below recursive call |
Logic: factorial(n) = n × factorial(n-1)
int getFactorial(int n) {
// Base case - mandatory
if(n == 0 || n == 1) {
return 1;
}
// Recursive call - mandatory
int recursionAns = getFactorial(n-1);
int finalAns = n * recursionAns;
return finalAns;
}