Recursive function
A Recursive function is that which calls itself directly or indirectly again and again is known as the recursive function. A Recursive functions are very useful although constructing the data structures such as linked lists, double linked lists or trees. There is a distinct difference among normal and recursive functions. A normal function will be invoked through the main function whenever the function name is used. The recursive function will be invoked through itself directly or indirectly as long as the given condition is satisfied.
Instance:
# include<iostream.h>
{
void main(void)
{
void f1 ( ); // function declaration
f1 ( ); // function calling
}
void f1( ) // function definition
{
f1 ( ); //function calls recursively
}
Sample program
# include<iostream.h>
void main( void)
{
int sum(int);
int n, temp;
cout<<"Enter any number"<< endl;
cin>>n;
temp = sum(n);
cout<<"value = "<<n<<"and its sum ="<< temp;
}
int sum ( int n)
{
int sum (int); // local function
int value = 0;
if (n= = 0)
return(value);
else
value = n+sum(n-1);
return (value);
}
output of the program is
Enter an integer number
4
value = 4 and its sum = 10