C Language

Function Recursion

In C Language by Baqir Ali

What is Recursion?

Recursion is the ability of a function to call itself, either directly or indirectly.

  • Causes a cycle
  • Recursion is a programming technique that naturally implements the divide-and-conquer programming methodology.

What is Recursive Function?

A recursive function does its task by calling upon itself. A recursive function must reduce the size of the remaining task on every call, or the process will become mired in an infinite repetition of the same process that is called infinite recursion.

How does a Recursive function work?

Resive Function
Resive Function

The Nature of Recursion

Problems that lend themselves to a recursive solution have the following characteristics:

  • One or more simple cases of the problem have a straightforward, non-recursive solution.
  • The other cases can be redefined in terms of problems that are closer to the simple cases.
  • By applying this redefinition process every time the recursive function is called, eventually the problem is reduced entirely to simple cases.

Iteration vs. Recursion

An iterative function which computes the sum of the first n positive numbers:

int sum(int n)
{
   int i, sum=0;
   for (i=1; i<=n; i++)
   sum = sum + i;
   return sum;
} 

Get The Coding Examples From C Language Code Example Page