A common example of recursion is the power function
Copy
public int timesTableR(int base,int exponent){
if(exponent<=0){
return 1;
}
return base*timesTableR(base,exponent-1);
}
Recursion is used in alot of applications.
Here are some notable ones
| Pros | Cons |
|---|---|
| Easy to Understand and requires minimal Statements | Slower than iterative and more memory intensive. As it leave a memory trail to the tail. |
| Preferred in problem with complex data structure like linked lists or graph theory | If Base case is not set, it can cause problems like crashing and stack overflow. |
| Useful in multiprogramming and multitasking enviroments | Each function will occupy memory in stack. This will lead to stack overflow error. |