Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Types of Recursion in C
Next article icon

Types of Recursion in C

Last Updated : 11 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Recursion is the process in which a function calls itself directly or indirectly to perform the same task it is doing but for some other data. It is possible by adding a call to the same function inside its body.

According to the number and position of this call, recursion can be classified into the following types:

Table of Content

  • Direct Recursion
    • Head Recursion
    • Tail Recursion
    • Tree Recursion
    • Nested Recursion
  • Indirect Recursion

Let's take a look at each of them one by one.

1. Direct Recursion

This is the simplest and most common form of recursion. In direct recursion, a function calls itself directly within its own body. For Example:

C
#include <stdio.h>  void show(int n) {     if (n == 0)         return;     printf("%d ", n);          // Direct recursive call     show(n - 1); }  int main() {     show(5);     return 0; } 

Output
5 4 3 2 1 

In this example, the show() function is directly calling itself with a smaller value of n. We can clearly see that the function call is present in its body.

Direct recursion can be further classified into two more types:

A. Head Recursion

In head recursion, the recursive call is made before any other statement in the function. So, the function first calls itself and then processes the result. For Example,

C
#include <stdio.h>  void head(int n) {     if (n != 0) {              // Recursive call before any processing         head(n - 1);     }     printf("%d ", n); }  int main() {     head(5);     return 0; } 

Output
0 1 2 3 4 5 

Here, the printing happens after the recursive call, which means the function first goes deep into recursion and then starts printing while returning.

head_recursion_in_c

B. Tail Recursion

Tail recursion is the opposite of head recursion. In this, the function performs its task first and then calls itself. The recursive call is the last operation in the function. For Example,

C
#include <stdio.h>  void tail(int n) {     if (n == 0)         return;     printf("%d ", n);          // Recursive call after processing     tail(n - 1); }  int main() {     tail(5);     return 0; } 

Output
5 4 3 2 1 
tail_recursion_in_c

Tail recursion is more memory-efficient than head recursion and can sometimes be optimized by the compiler. To know more, refer to the article - Tail Call Optimisation in C - GeeksforGeeks

C. Tree Recursion

In tree recursion, a function makes more than one recursive call to itself within its body. As a result, the recursion tree branches out. For Example,

C
#include <stdio.h>  void tree(int n) {     if (n == 0)         return;     printf("%d ", n);          // First recursive call     tree(n - 1);          // Second recursive call     tree(n - 1); }  int main() {     tree(3);     return 0; } 

Output
3 2 1 1 2 1 1 
tree_recursion_in_c

As you can see, the function is called twice for each value, which leads to a tree-like recursive structure.

D. Nested Recursion

Nested recursion occurs when a recursive function’s argument itself is a recursive function call. For Example,

C
#include <stdio.h>  int nested(int n) {     if (n > 100)         return n - 10;              // Two recursive calls nested inside     // each other.     return nested(nested(n + 11)); }  int main() {     printf("%d", nested(95));     return 0; } 

Output
91

This type of recursion is more complex and is rarely used unless required for specific logic.

2. Indirect Recursion

In indirect recursion, a function doesn’t call itself directly. Instead, it calls another function, which in turn calls the first one. This chain can involve more than two functions. For example,

C
#include <stdio.h>  void funcA(int); void funcB(int);  void funcA(int n) {     if (n > 0) {         printf("%d ", n);         funcB(n - 1);     } }  void funcB(int n) {     if (n > 0) {         printf("%d ", n);         funcA(n / 2);     } }  int main() {     funcA(10);     return 0; } 

Output
10 9 4 3 1 

Here, funcA() calls funcB(), and funcB() calls funcA(). They are indirectly recursive.

indirect_recursion_in_c



Next Article
Types of Recursion in C

A

abhishekcpp
Improve
Article Tags :
  • C Language
  • C-Functions

Similar Reads

    C Recursion
    Recursion is the process of a function calling itself repeatedly till the given condition is satisfied. A function that calls itself directly or indirectly is called a recursive function and such kind of function calls are called recursive calls.Example:C#include <bits/stdc++.h> using namespac
    7 min read
    Return Statement in C
    Pre-requisite: Functions in C C return statement ends the execution of a function and returns the control to the function from where it was called. The return statement may or may not return a value depending upon the return type of the function. For example, int returns an integer value, void retur
    4 min read
    Derived Data Types in C
    Data types in the C language can be categorized into three types, namely primitive, user-defined, and derived data types. In this article, we shall learn about derived data types. In C, the data types derived from the primitive or built-in data types are called Derived Data Types. In other words, th
    4 min read
    Types of User Defined Functions in C
    A user-defined function is one that is defined by the user when writing any program, as we do not have library functions that have predefined definitions. To meet the specific requirements of the user, the user has to develop his or her own functions. Such functions must be defined properly by the u
    4 min read
    Finite and Infinite Recursion with examples
    The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences