Recursive function

A function calls itself repeatedly until certain condition specified is called function recursion. In C, all functions can be used recursively.

Below we will use of function recursion with an example.

Write a simple example to print “welcome to gyantoday.com” 5 times.

#include<stdio.h>

void func(int count)

{

            while (count < 5)

            {

                        printf(“welcome to gyantoday.com\n”);

                        count++;

            }

}

int main()

{

            int count = 0;

            func();

            return 0;

}

Now we will write a program for same output using recursive function.

#include<stdio.h>

void func(int count)

{

            if (count < 5)

            func(count+1);

            printf(“welcome to gyantoday.com\n”);

}

int main()

{

            int count = 0;

            func();

            return 0;

}

We will see extensive use of function recursion in DATA STRUCTURE, meanwhile giving below some example of use of function recursion.

  1. Find the factorial of a given number using recursion.
  2. Fibonacci series using recursion.
  3. Write a C program for palindrome using recursion.
  4. Reverse a string using recursion.
  5. Find the power of a number using recursion.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.