Computer Science‎ > ‎

Recursion


A Recursive function is one which calls itself as part of an operation or computation. It should always have a base condition when it will terminate execution.

Example :  

A function to compute factorials : 

int factorial( int N ) 
{
    if ( N == 1 ) return 1 ; 
    else return N * factorial ( N - 1 ) ;
}

Another example to compute fibonacci values recursively :

int fibonacci(int N)
{
    if ( N == 0 or 1 ) return 1 ;
    else return fibonacci(N-1) + fibonacci(N-2);
}

Demonstrating a very simple case of recursion to compute factorials : 

ċ
recursiveFactorial.jar
(5k)
Prashant Bhattacharji,
Aug 2, 2011, 3:05 AM
Comments