What is Recursion ?
              Recursion is a programming technique in which a function calls itself to solve a problem.

A recursive function breaks a big problem into:

  • smaller sub-problems
  • until a stopping condition is reached

Each recursive call works on a smaller input.


Syntax :

Python
def function_name(parameters): if base_condition: return result return function_name(smaller_input) 

recursive call : function calling itself
base case : stopping condition
smaller input : problem size reduces each call


Why Base case is important ?

Without a base case → recursion runs forever → leads to infinite recursion or stack overflow error


Example :

Factorial

Python
def factorial(n): if n == 1: return 1 return n * factorial(n - 1) print(factorial(5)) #output 120 


Fibonacci Series :

Python
def fibonacci(n): if n <= 1: return n return fibonacci(n-1) + fibonacci(n-2) print(fibonacci(6)) #output 8