Recursion
Self calling functions. i.e inside the definition of a function, the function is called.
Repiteadly doing the same thing again and again is recursion.
Solving bigger problem using smaller problems is recursion.
graph TD A[Start] --> B(Recursive Function) B --> C{Base Case?} C -- Yes --> D[Return Base Case Value] C -- No --> E[Recursive Call] E --> B D --> F[End]
Recursive programs have a termination condition and a base case. If there is no base case, the program will run indefinitely and cause stack to overflow.
function call means push in stack and function return means pop from stack.
As soon as a fucntion is called it’s activiation record is pushed onto the stack.
Number of stack units required for a recursive program is directly proportional to the depth of the recursion tree.
In comparision a non-recursive program requires less or equal stack space.
Variables in recursion:
- Arguements
- Return Value
- Body of the function
To understand and approach a recursive program:
- Understand the problem statement and break it into smaller problems.
- Figure out recurrence relation
- Make stack or recursion tree to understand the flow of the program
- Use a debugger
- See how and what type of values are returned at each step. See where the fucntion call comes out with each value.