- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion

# Principles of Recursion in Data Structures

The recursion is a process by which a function calls itself. We use recursion to solve bigger problem into smaller sub-problems. One thing we have to keep in mind, that if each sub-problem is following same kind of patterns, then only we can use the recursive approach.

A recursive function has two different parts. The base case and the recursive case. The base case is used to terminate the task of recurring. If base case is not defined, then the function will recur infinite number of times (Theoretically).

In computer program, when we call one function, the value of the program counter is stored into the internal stack before jumping into the function area. After completing the task, it pops out the address and assign it into the program counter, then resume the task. During recursive call, it will store the address multiple times, and jumps into the next function call statement. If one base case is not defined, it will recur again and again, and store address into stack. If the stack has no space anymore, it will raise an error as “Internal Stack Overflow”.

One example of recursive call is finding the factorial of a number. We can see that the factorial of a number n = n! is same as the n * (n-1)!, again it is same as n * (n - 1) * (n - 2)!. So if the factorial is a function, then it will be called again and again, but the argument is decreased by 1. When the argument is 1 or 0, it will return 1. This could be the base case of the recursion.

## Example

#include<iostream> using namespace std; long fact(long n){ if(n <= 1) return 1; return n * fact(n-1); } main(){ cout << "Factorial of 6: " << fact(6); }

## Output

Factorial of 6: 720

- Related Articles
- Tail Recursion in Data Structures
- Abstract Data Type in Data Structures
- Comparison of Searching methods in Data Structures
- Comparison of Sorting methods in Data Structures
- Kernel Data Structures
- Kinetic Data Structures
- Inbuilt Data Structures in C#
- Inbuilt Data Structures in Python
- Bernoulli Distribution in Data Structures
- Binomial Distribution in Data Structures
- Geometric Distribution in Data Structures
- Stack ADT in Data Structures
- Adjacency lists in Data Structures
- Data structures of a Windows thread
- In-built Data Structures in Python