C Recursion

Recursion is an function calling process. When function is calling itself then that is called direct recursion. And when function are calling other function then that is called indirect recursion. Recursion are suitable to solve small and complex problem by dividing sub problem. Recursion are always need a base condition to stop execution of function calling. There are is an important points to control recursion.

Types of Recursion

Normally recursion are two types in C Programming. And that is based on function calling process.

1) Direct Recursion

When function calling itself during program execution that is called direct recursion. One of best example of direct recursion are calculated factorial of given number. Here given an example.

Direct recursion
/*
   Example of Direct recursion
*/
//header file
#include <stdio.h>

//function declaration
int factorial(int);

//Calculate factorial of given number
int factorial(int num){
  if(num==0){ return 1;}
  else {
    return factorial(num-1)*num;//Recursive call
  }
}
int main(){
  int num=5;
  printf(" factorial of %d is : %d", num,factorial(num)); //function call
}
Example of Direct recursion

Output:

factorial of 5 is : 120

2) Indirect recursion

indirect recursion functional calling execution are not execute same function always, This are execute two or more than two function in some intervals. Indirect recursion also called mutual recursion.

In-direct recursion

Example

/*
   Example of Indirect recursion
*/
//Include header file
#include <stdio.h>

//Function declaration
void funct1(int);
void funct2(int);

void funct1(int num){
  if(num==0){
      //base condition
    return ;
  }
  printf("\n funct1 num : %d",num);
  funct2(num-1);//indirect function call
}
void funct2(int num){
  if(num==0){
      //Base condition
    return ;
  }
  printf("\n funct2 num : %d",num);
    funct1(num-1); //indirect function call
}

int main(){
  //function calling
  funct1(4);
  return 0;
}

Output

Example of Direct recursion

Output:

funct1 num : 4
 funct2 num : 3
 funct1 num : 2
 funct2 num : 1

Memory allocation of Recursive Function

Recursion execution is automatic memory handle mechanism. when calling of any user defined function stack are allocates memory to this function. This memory are contain information of local variable of function. Memory are free of this function automatically when function is execute all statements. For example.

/*
    Example of recursion are use stack memory
*/
//header file
#include <stdio.h>
//function declaration
void display(int);

void display(int value){
    if(value>10){
        //base condition
        return;
    }else{  
        printf("\n %d",value); //print value
        display(value+1);//recurive call
    }
}

int main(){
    //Start execution of 
    display(5);
    return 0;
}
Recursion stack memory

Output

 5
 6
 7
 8
 9
 10

In this program display() function is recursively execute 6 times. And every time is creating the memory in stack area for this function.

Advantages of Recursion

1) To write an efficient program with minimum amount of codes (line of codes).

2) Solving a complex program in very easily. Example permutations and combination.

3) Automatic memory management.

Disadvantage of Recursion

1) If program have logical error then possible to produce infinite loop. Or in case not given end terminating condition then continuously recursion are executed. and program can be crashed.

2) Recursion is a function calling process. When call a function then compiler are allocate memory of this function in stack area, this area are reserved when not execute all instruction of this function. So this process are use more space comparing to iterative solution.

3) Calling multiple recursive function then it will difficult to finding bugs.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved