CodesandTutorials
Next generation website theme is here (black, neon and white color) - Reduces eye strain and headache problems.
You are here - Home > Programming > C > Basics

Recursive Function


Definition

A function which calls itself is called as recursive function.

Description

  • Are slower in speed becuase of stack overhead.
  • Should have recursive expression with terminating statement.

Source code

  • //Finding the factorial of given number using recursive function.
  • #include <conio.h>
  • #include <stdio.h>
  • void main()
  • {
  • int number,fact;
  • clrscr();
  • printf("Enter the number:");
  • scanf("%d",&number);
  • fact=factorial(number);
  • printf("Factorial of given number is %d.",fact);
  • getch();
  • }
  • int factorial(int n)
  • {
  • int f=1;
  • if(n<2)
  • return f;
  • else
  • {
  • f = n*factorial(n-1);
  • return f;
  • }
  • }

Program working steps

  • First line is the comment, 2nd and 3rd line are preprocessor directives which holds definition of functions like clrscr(), printf(), scanf() etc.
  • void main() is the funtion, where program execution starts.
  • Declaration of variable number to store input number and fact to display the required result.
  • clrscr() function to clear the screen like cls in windows and clear in linux.
  • Next two statements takes the input from the user and stores the value into variable number.
  • Line no.11 calls the factorial function by passing number variable to it. So the program control transfers to line no.15, where a block of code for calculating the factorial of a number is written.
  • factorial function takes one argument from the caller function and stores the value into the variable n of integer data-type.
  • First is the terminating condition if(n<2) then break recursive loop, else part contains recursive expression return n*factorial(n-1) which calls itself.
  • Suppose the user enters 4 as a value then the factorial function will work like,
    Fist the terminating condition
    if(4<2) is false so it will execute else part
    4*factorial(4-1)
    this statement calls the factorial function with(4-1)as a argument, so it will again start from first statement.
    if(3<2) now the value of n is decrease by one, but still condition is false so it will execute else part.
    3*factorial(3-1) This statement again calls itself.
    This will keep repeating till terminating condition becomes true i.e. when n = 1. Then the function will return the calculated value using return f; statement.
    A closer lookup of the recursive calls
    f=4*factorial(4-1);
    f=4*3*factorial(3-1);
    f=4*3*2*factorial(2-1);
    f=4*3*2*1;
    f=16;
  • This calculated f value will be return back to calling functoin to line no.11, where value of fact will be 16.
  • Next printf statement prints the quoted text, 'Factorial of given number is 16.'.
  • getch() to wait input character to exit the program.

Tip Box

Remember

Recursive function should have exit condition to avoid stack overlfow.


Advertisement





Terms of Use | Privacy Policy | Contact Us | Advertise
CodesandTutorials © 2014 All Rights Reserved