Hello friends, in today’s article we are going to talking about Recursion in C Language

Today we will learn what is recursion? And what are the uses of recursion in the C language? But to know about Recursion well, you need to know in advance about these topics given below.

If you do not know this topic, then you must read about it once and then read this article.

So without wasting time let’s understand what is Recursion In C

Page Contents

## What is Recursion In C

As you know that any function in the C language can call any function. So a function can also call itself and when a function calls itself, then it is called Recursion. Such functions are called recursive functions and such calls are called recursive calls.

A recursive function can call itself any number of times, but its exit condition must be defined. If we do not define its exit condition then it will become an infinite loop and the program will never end.

Recursive functions are very useful in many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

With the help of Recursion, we do not solve all kinds of problems. With its help, we solve only such problems, which we can divide into subtasks.

**For Example **– Recursion is used to solve problems like sorting, searching, traversal problems, etc.

When we solve a problem with the help of Recursion, we have to write much less code than iterative code, although its code is a bit difficult to understand.

With the help of loop, We can easily solve all the problems that can be solved with the help of recursion, but with the help of recursion we cannot solve all the problems that can be solved with the help of loop.

Generally, when we solve a problem with the help of iterative method i.e. loop, then it is more efficient than solving that problem by recursion.

However, recursion is more efficient in solving some problems like Towers of Hanoi (TOH), Inorder/Preorder/ Postorder Tree Traversals, DFS of Graph etc.

## Syntax of recursion

```
void recursion()
{
recursion(); /* function calls itself */
}
int main()
{
recursion();
}
```

## Example Program of Recursion

```
#include <stdio.h>
int sum(int n);
void main()
{
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum = %d", result);
}
int sum(int n)
{
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
```

**Output -:**

```
Enter a positive integer:3
sum = 6
```

In this program, we have created a program to add natural numbers, in which the sum() function calls itself many times and this is the recursion. In this program sum() is a recursive function.

## Advantages of Recursion

- Recursion code is easy to write
- Code can be written in very few lines.
- The problem of recursive tendency can be easily solved.
- Reduces unnecessary calls to functions
- Towers of Hanoi (TOH) is very useful for solving data structure problems with the help of recursion.

## Disadvantages of Recursion

- Recursive functions are much slower than non-recursive functions.
- Recursive function takes more space in memory.
- It becomes a bit difficult to analyze and understand the code.
- It is not more efficient in terms of memory savings.
- Program run time increases.

**Read More -:**

- Introduction of Function in C
- Function Arguments in C
- Actual Argument and Formal Argument in C
- Command Line Arguments in C
- Call by value and Call by reference in C
- Storage Classes in C

## Conclusion

Friends, I hope that after reading this article you know very well, what is Recursion in C Language?

If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.

If you liked this post, then do not forget to share this post with your friends so that they can also get information about Recursion in C Language

If you still have any questions or doubts related to Recursion, then tell me in the comment box, I will answer all your questions and you can contact us for more information.

Subscribe to this website newsletter to get information related to this new technology, Programming Language, Coding, C Language, C++, Python Course, Java Tutorial.