Wednesday 8 May 2013

Recursion (Recursive Function) :


Recursion (Recursive Function) :
When a function of body calls the same function then it is called as 'recursive function.'

Example:

Recursion()
{
        printf("Recursion !");
        Recursion();
}

Program :


#include <stdio.h>
#include <conio.h>
 
Recursion()
{
        int no;
        printf("\nRecursion... ");
        printf("\n\n Enter Number : ");
        scanf("%d",&no);
        if (no==3)
               exit(0);
        else
               Recursion();
}
void main()
{
        clrscr();
        Recursion();
}

Output :



 
Recursion...
 
 Enter Number : 2
 
Recursion...
 
 Enter Number : 1
 
Recursion...
 
 Enter Number : 3_



Features :

·         There should be at least one if statement used to terminate recursion.
·         It does not contain any looping statements.

Advantages :

·         It is easy to use.
·         It represents compact programming strctures.

Disadvantages :

·         It is slower than that of looping statements because each time function is called.

Note :

·         It can be applied to calculate factorial of a number, fibonancci series.

Functions in C :


The function is a self contained block of statements which performs a coherent task of a same kind.
C program does not execute the functions directly. It is required to invoke or call that functions. When a function is called in a program then program control goes to the function body. Then, it executes the statements which are involved in a function body. Therefore, it is possible to call fuction whenever we want to process that functions statements.

Types of functions :
There are 2(two) types of functions as:
1. Built in Functions
2. User Defined Functions

1. Built in Functions :
These functions are also called as 'library functions'. These functions are provided by system. These functions are stored in library files. e.g.

·         scanf()
·         printf()
·         strcpy
·         strlwr
·         strcmp
·         strlen
·         strcat

2. User Defined Functions :
The functions which are created by user for program are known as 'User defined functions'.

Syntax:

void main()
{
        // Function prototype
        <return_type><function_name>([<argu_list>]);
       
        // Function Call
        <function_name>([<arguments>]);
}
// Function definition
<return_type><function_name>([<argu_list>]);
{
        <function_body>;
}

Program :


#include <stdio.h>
#include <conio.h>
 
void add()
{
        int a, b, c;
        clrscr();
        printf("\n Enter Any 2 Numbers : ");
        scanf("%d %d",&a,&b);
        c = a + b;
        printf("\n Addition is : %d",c);
}
void main()
{
        void add();
        add();
        getch();
}

Output :



 Enter Any 2 Numbers : 23 6
 Addition is : 29_

Advantages :
·         It is easy to use.
·         Debugging is more suitable for programs.
·         It reduces the size of a program.
·         It is easy to understand the actual logic of a program.
·         Highly suited in case of large programs.
·         By using functions in a program, it is possible to construct modular and structured programs.