The Wayback Machine - https://web.archive.org/web/20240717180321/https://www.geeksforgeeks.org/what-is-recursion/
Open In App

What is Recursion?

Last Updated : 13 Mar, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.

Properties of Recursion:

Recursion has some important properties. Some of which are mentioned below:

  • The primary property of recursion is the ability to solve a problem by breaking it down into smaller sub-problems, each of which can be solved in the same way.
  • A recursive function must have a base case or stopping criteria to avoid infinite recursion.
  • Recursion involves calling the same function within itself, which leads to a call stack.
  • Recursive functions may be less efficient than iterative solutions in terms of memory and performance.

Types of Recursion:

  1. Direct recursion: When a function is called within itself directly it is called direct recursion. This can be further categorised into four types: 
    • Tail recursion,  
    • Head recursion,  
    • Tree recursion and 
    • Nested recursion.
  2. Indirect recursion: Indirect recursion occurs when a function calls another function that eventually calls the original function and it forms a cycle.

To learn more about types of recursion, refer to this article.

Applications of Recursion:

Recursion is used in many fields of computer science and mathematics, which includes:

  • Searching and sorting algorithms: Recursive algorithms are used to search and sort data structures like trees and graphs.
  • Mathematical calculations: Recursive algorithms are used to solve problems such as factorial, Fibonacci sequence, etc.
  • Compiler design: Recursion is used in the design of compilers to parse and analyze programming languages.
  • Graphics: many computer graphics algorithms, such as fractals and the Mandelbrot set, use recursion to generate complex patterns.
  • Artificial intelligence: recursive neural networks are used in natural language processing, computer vision, and other AI applications.

Advantages of Recursion:

  • Recursion can simplify complex problems by breaking them down into smaller, more manageable pieces.
  • Recursive code can be more readable and easier to understand than iterative code.
  • Recursion is essential for some algorithms and data structures.
  • Also with recursion, we can reduce the length of code and become more readable and understandable to the user/ programmer.

Disadvantages of Recursion:

  • Recursion can be less efficient than iterative solutions in terms of memory and performance.
  • Recursive functions can be more challenging to debug and understand than iterative solutions.
  • Recursion can lead to stack overflow errors if the recursion depth is too high.

What else can you read?


Previous Article
Next Article

Similar Reads

Why is Tail Recursion optimization faster than normal Recursion?
What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recursive function in which the recursive call is the f
4 min read
The Great Tree-List Recursion Problem.
Asked by Varun Bhatia. Question: Write a recursive function treeToList(Node root) that takes an ordered binary tree and rearranges the internal pointers to make a circular doubly linked list out of the tree nodes. The”previous” pointers should be stored in the “small” field and the “next” pointers should be stored in the “large” field. The list sho
1 min read
Inorder Tree Traversal without recursion and without stack!
Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree. In this traversal, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree. 1. Initialize current as root 2. While current
9 min read
Practice questions for Linked List and Recursion
Assume the structure of a Linked List node is as follows. C/C++ Code struct Node { int data; struct Node *next; }; // This code is contributed by SHUBHAMSINGH10 C/C++ Code struct Node { int data; struct Node *next; }; Java Code static class Node { int data; Node next; }; // This code is contributed by shubhamsingh10 C/C++ Code class Node: def __ini
11 min read
Practice Questions for Recursion | Set 1
Explain the functionality of the following functions. Question 1 C/C++ Code int fun1(int x, int y) { if (x == 0) return y; else return fun1(x - 1, x + y); } C/C++ Code int fun1(int x, int y) { if (x == 0) return y; else return fun1(x - 1, x + y); } Java Code static int fun1(int x, int y) { if (x == 0) return y; else return fun1(x - 1, x + y); } C/C
5 min read
Practice Questions for Recursion | Set 2
Explain the functionality of the following functions. Question 1 C/C++ Code /* Assume that n is greater than or equal to 1 */ int fun1(int n) { if (n == 1) return 0; else return 1 + fun1(n / 2); } Java Code /* Assume that n is greater than or equal to 1 */ static int fun1(int n) { if (n == 1) return 0; else return 1 + fun1(n / 2); } C/C++ Code # As
3 min read
Practice Questions for Recursion | Set 4
Question 1 Predict the output of the following program. C/C++ Code #include <iostream> using namespace std; void fun(int x) { if(x > 0) { fun(--x); cout << x <<" "; fun(--x); } } int main() { int a = 4; fun(a); return 0; } // This code is contributed by SHUBHAMSINGH10 C/C++ Code #include<stdio.h> void fun(int x)
6 min read
Practice Questions for Recursion | Set 5
Question 1Predict the output of the following program. What does the following fun() do in general? C/C++ Code #include <iostream> using namespace std; int fun(int a, int b) { if (b == 0) return 0; if (b % 2 == 0) return fun(a + a, b/2); return fun(a + a, b/2) + a; } int main() { cout << fun(4, 3) ; return 0; } // This code is contribut
5 min read
Practice Questions for Recursion | Set 6
Question 1 Consider the following recursive C function. Let len be the length of the string s and num be the number of characters printed on the screen. Give the relation between num and len where len is always greater than 0. C/C++ Code void abc(char *s) { if(s[0] == '\0') return; abc(s + 1); abc(s + 1); cout << s[0]; } // This code
4 min read
Practice Questions for Recursion | Set 7
Question 1 Predict the output of the following program. What does the following fun() do in general? C/C++ Code #include <iostream> using namespace std; int fun(int n, int* fp) { int t, f; if (n <= 2) { *fp = 1; return 1; } t = fun(n - 1, fp); f = t + *fp; *fp = t; return f; } int main() { int x = 15; cout << fun(5, &x) <<
4 min read
Practice Tags :
three90RightbarBannerImg