Difference between revisions of "Interview Preparation topic: Recursive Function"

From Embedded Systems Learning Academy
Jump to: navigation, search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
'''RECURSION'''
 +
 +
 
Recursion is the process of repeating items in a self-similar way.
 
Recursion is the process of repeating items in a self-similar way.
  
 
If a program allows you to call a function inside the same function, then it is called a recursive call of the function.
 
If a program allows you to call a function inside the same function, then it is called a recursive call of the function.
  
<pre>
+
<syntaxhighlight lang="C">
 
void recursion()  
 
void recursion()  
 
   {
 
   {
 +
 
   recursion(); /* function calls itself */
 
   recursion(); /* function calls itself */
 +
 
   }
 
   }
  
 
int main()  
 
int main()  
 
   {
 
   {
 +
 
   recursion();
 
   recursion();
 +
 +
  }
 +
</syntaxhighlight>
 +
 +
While using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
 +
 +
The recursion continues until some condition is met to prevent it.
 +
 +
To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch makes the recursive call and other doesn't.
 +
 +
Recursive functions are very useful to solve many mathematical problems, such as calculating sum of natural numbers, calculating the factorial of a number, generating Fibonacci series, etc.
 +
 +
 +
'''Example 1: SUM OF NATURAL NUMBERS'''
 +
 +
 +
The following example calculates the sum of natural numbers using a recursive function:
 +
 +
<syntaxhighlight lang="C">
 +
#include <stdio.h>
 +
 +
int sum(int n);
 +
 +
int main()
 +
  {
 +
 +
  int result;
 +
  int number = 5;
 +
  result = sum(number);
 +
  printf("Sum of %d Natural Numbers Using Recursion is %d\n",number, result);
 +
 
   }
 
   }
 +
 +
int sum(int num)
 +
  {
 +
 
 +
  if (num != 0)
 +
      {
 +
      return num + sum(num - 1); // sum() function calls itself
 +
      } 
 +
  else
 +
      {
 +
      return num;
 +
      }
 +
 
 +
  }
 +
</syntaxhighlight>
 +
 +
When the above code is compiled and executed, it produces the following result:
 +
<pre>
 +
Sum of 5 Natural Numbers Using Recursion is 15
 
</pre>
 
</pre>
  
While using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
+
Initially, the sum() is called from the main() function with number passed as an argument.
 +
 
 +
Suppose, the value of num is 5 initially. During next function call, 4 is passed to the sum() function. This process continues until num is equal to 0.
  
Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.
+
When num is equal to 0, the if condition fails and the else part is executed returning the sum of integers to the main() function.
  
  
'''FACTORIAL OF A NUMBER'''
+
'''Example 2: FACTORIAL OF A NUMBER'''
  
  
 
The following example calculates the factorial of a given number using a recursive function:
 
The following example calculates the factorial of a given number using a recursive function:
  
<pre>
+
<syntaxhighlight lang="C">
 
#include <stdio.h>
 
#include <stdio.h>
  
Line 32: Line 90:
 
int  main()  
 
int  main()  
 
   {
 
   {
 +
 
   int number = 5;
 
   int number = 5;
 
   printf("Factorial of %d is %d\n", number, factorial(number));
 
   printf("Factorial of %d is %d\n", number, factorial(number));
 
   return 0;
 
   return 0;
 +
 
   }
 
   }
  
Line 46: Line 106:
  
 
   return i * factorial(i - 1);
 
   return i * factorial(i - 1);
 +
 
   }
 
   }
</pre>
+
</syntaxhighlight>
  
 
When the above code is compiled and executed, it produces the following result:
 
When the above code is compiled and executed, it produces the following result:
Line 55: Line 116:
  
  
'''FIBONACCI SERIES'''
+
'''Example 3: FIBONACCI SERIES'''
  
  
 
The following example generates the Fibonacci series for a given number using a recursive function:
 
The following example generates the Fibonacci series for a given number using a recursive function:
<pre>
+
<syntaxhighlight lang="C">
 
#include <stdio.h>
 
#include <stdio.h>
  
Line 66: Line 127:
 
int  main()  
 
int  main()  
 
   {
 
   {
 +
 
   int number=8;
 
   int number=8;
 
 
Line 74: Line 136:
 
 
 
   return 0;
 
   return 0;
 +
 
   }
 
   }
  
Line 90: Line 153:
  
 
   return fibonacci(i-1) + fibonacci(i-2);
 
   return fibonacci(i-1) + fibonacci(i-2);
 +
 
   }
 
   }
</pre>
+
</syntaxhighlight>
  
 
When the above code is compiled and executed, it produces the following result:
 
When the above code is compiled and executed, it produces the following result:
Line 99: Line 163:
  
  
'''SUM OF NATURAL NUMBERS'''
 
 
The following example calculates the sum of natural numbers using a recursive function:
 
<pre>
 
#include <stdio.h>
 
 
int sum(int n);
 
 
int main()
 
  {
 
  int number, result;
 
  number = 5;
 
  result = sum(number);
 
  printf("Sum of %d Natural Numbers Using Recursion is %d\n",number, result);
 
  }
 
 
int sum(int num)
 
  {
 
 
 
  if (num != 0)
 
      {
 
      return num + sum(num - 1); // sum() function calls itself
 
      } 
 
  else
 
      {
 
      return num;
 
      }
 
 
 
  }
 
</pre>
 
 
When the above code is compiled and executed, it produces the following result:
 
<pre>
 
Sum of 5 Natural Numbers Using Recursion is 15
 
</pre>
 
  
 
'''Discussions'''
 
'''Discussions'''

Latest revision as of 22:57, 18 December 2016

RECURSION


Recursion is the process of repeating items in a self-similar way.

If a program allows you to call a function inside the same function, then it is called a recursive call of the function.

void recursion() 
   {

   recursion(); /* function calls itself */

   }

int main() 
   {

   recursion();

   }

While using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.

The recursion continues until some condition is met to prevent it.

To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch makes the recursive call and other doesn't.

Recursive functions are very useful to solve many mathematical problems, such as calculating sum of natural numbers, calculating the factorial of a number, generating Fibonacci series, etc.


Example 1: SUM OF NATURAL NUMBERS


The following example calculates the sum of natural numbers using a recursive function:

#include <stdio.h>

int sum(int n);

int main()
   {

   int result;
   int number = 5;
   result = sum(number);
   printf("Sum of %d Natural Numbers Using Recursion is %d\n",number, result);

   }

int sum(int num)
   {
   
   if (num != 0)
      {
      return num + sum(num - 1); // sum() function calls itself
      }   
   else
      {
      return num;
      }
   
   }

When the above code is compiled and executed, it produces the following result:

Sum of 5 Natural Numbers Using Recursion is 15

Initially, the sum() is called from the main() function with number passed as an argument.

Suppose, the value of num is 5 initially. During next function call, 4 is passed to the sum() function. This process continues until num is equal to 0.

When num is equal to 0, the if condition fails and the else part is executed returning the sum of integers to the main() function.


Example 2: FACTORIAL OF A NUMBER


The following example calculates the factorial of a given number using a recursive function:

#include <stdio.h>

int factorial(unsigned int i);

int  main() 
   {

   int number = 5;
   printf("Factorial of %d is %d\n", number, factorial(number));
   return 0;

   }

int factorial(unsigned int i) 
   {

   if(i <= 1) 
      {
      return 1;
      }

   return i * factorial(i - 1);

   }

When the above code is compiled and executed, it produces the following result:

Factorial of 5 is 120


Example 3: FIBONACCI SERIES


The following example generates the Fibonacci series for a given number using a recursive function:

#include <stdio.h>

int fibonacci(int i); 

int  main() 
   {

   int number=8;
	
   for (i = 0; i < number-1; i++) 
      {
      printf("%d\t\n", fibonacci(i));
      }
	
   return 0;

   }

int fibonacci(int i) 
   {
   
   if(i == 0) 
      {
      return 0;
      }
	
   if(i == 1) 
      {
      return 1;
      }

   return fibonacci(i-1) + fibonacci(i-2);

   }

When the above code is compiled and executed, it produces the following result:

0	1	1	2	3	5	8	


Discussions