Recursion in C Programming

Recursion in c programming
Share on facebook
Share on twitter
Share on linkedin

What Is Recursion?

#include <stdio.h>
/**
 * factorial - find the factorial of a given number
 * @n: an argument passed to the function
 *
 * Return: the factorial of a given number, -1 if lower than 0
 */
int factorial(int n)
{
	if (n < 0)
	{
		return (-1);
	}
	else if (n < 1)
	{
		return (1);
	}
	return (n * factorial(n - 1));
}

Just like in loops which utilizes iteration to repetitively run a block of code based on a condition, recursion in the same way is a kind of repetition of a block of code too. But there’s a lot of difference between these two methods. So what’s recursion?

Recursion is a method where the solution of a bigger problem depends on the solutions to smaller instances of the same problem. And in C programming language, a recursive function is one that calls itself.

When you implement recursion in your code to find the factorial of a number like say, 4; what happens behind the scene is that the code first asks for the factorial of 3, and 3 will of 2, and finally 2 of 1.

TAKEAWAY: 1. A function calling itself inside itself is called recursion. 2. Factorial is the product of an integer and all the integers below it.

When Do You Need Recursion

Whenever you have a problem (of math or programmable nature) and such problem can be broken down into smaller chunks of other problems that their commutative solutions will equal the solution of the bigger problem. Such calculations as getting the factorial of a number, finding Fibonacci series etc, can be solved with a recursion rather than iteration.

Also, tasks that would rather be too complex for the iterative approach of a loop can be easily implemented with a recursive function in C programming.

How Do You Implement Recursion

To write a recursive function in C programming, you start off by the functions prototype.

This is where you specify the functions return type, name of function, number of parameters, the order of arguments and their types.

With your prototype in place, you just need one more important first lines of code called the base statements.

Without the base statement, the function will go into an infinite loop (just like an ordinary loop) and eventually crashes.

When You Don’t Need Recursion

Even as handy and great tool recursion is, there are cases you don’t need a recursive function in C programming language.

The same complex tasks that recursion tend to make simpler by minimizing the lines of code than in a loop, it turns out that even too complex tasks are also not suitable for recursion in C.

The reason is that with recursion, every instance of the computation of the solution to a smaller chunk of the bigger problem will require a separate stack frame.

This in essence costs more resources to execute and takes more time during execution.

TAKEAWAY: 1. Stack is a concept used in memory organization in computers

Summary

In summary, if code simplicity is most important in your project, and a case of stack getting overflowed is not to be taking seriously, then do use recursion. Else, by all means go for loop (iteration).

Happy coding.

>
error: Alert: Content is protected !!