What is Recursion ?
Recursion is a process where a function calls itself either directly or indirectly, and the function used in this process is called a recursive function. Recursive algorithms can solve specific problems easily, such as Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. Recursive functions solve a problem by calling a copy of itself, which solves smaller subproblems of the original problem, generating more recursive calls as needed.However, it’s crucial to provide a case that terminates this recursive process. In other words, every time the function calls itself, it does so with a simpler version of the original problem.
Recursion has certain advantages over iteration technique, such as reducing the length of code and making it more readable and easier to write. It is a suitable solution for tasks that can be defined with similar subtasks. For instance, finding the factorial of a number can be solved using recursion.
In summary, recursion is a powerful technique that can solve specific problems efficiently. It has some unique properties that make it a useful tool in programming, especially when dealing with tasks that involve similar subtasks.
The syntax for implementing recursion in C programming is as follows:
return_type functionName(parameters) { if (baseCase) { // terminate recursion return someValue; } else { // recursive case return functionName(modifiedParameters); } }
In this syntax, return_type
specifies the type of value returned by the function, functionName
is the name of the recursive function, and parameters
are the input parameters passed to the function.
The if
statement checks for a base case that terminates the recursion, and if the base case is not met, the function calls itself with modifiedParameters
. The modified parameters are used to solve a smaller version of the original problem, which eventually leads to the base case being met.
It’s important to ensure that the recursive function has a base case that terminates the recursion. Otherwise, the function will continue to call itself indefinitely, causing the program to crash or run out of memory.
Example of a recursive function that calculates the factorial of a number in C programming:
#include <stdio.h> // Recursive function to calculate factorial int factorial(int n) { if (n == 0) { // Base case: factorial of 0 is 1 return 1; } else { // Recursive case: factorial of n is n times factorial of n-1 return n * factorial(n-1); } } // Main function to call factorial function int main() { int num, result; printf("Enter a number to calculate its factorial: "); scanf("%d", &num); result = factorial(num); printf("Factorial of %d is %d", num, result); return 0; }
This program uses a recursive function to calculate the factorial of a number ‘n’. The function initially checks for a base case where the value of ‘n’ is zero and returns the result as 1. However, if the base case is not met, the function recursively calls itself with the argument as ‘n-1’ and multiplies the result with the original value of ‘n’ to calculate the factorial of ‘n’.
The main function of the program interacts with the user by prompting them to enter a number. Once the user provides the input value, the program uses the ‘scanf’ function to read the value from the console. The main function then passes the input value to the recursive ‘factorial’ function to calculate its factorial. Finally, the program uses the ‘printf’ function to display the calculated result on the console.
When you run this program and enter a number, it will use recursion to compute the factorial of the number and show the result on the console.
Output
Enter a number to calculate its factorial: 5
Factorial of 5 is 120
When you run the program, it will prompt the user to enter a number. If the user enters 5 as the input value, the program will call the factorial
function with 5 as the argument. The factorial
function will calculate the factorial of 5 using recursion, and the result will be 120. Finally, the program will use the printf
function to display the result on the console, which shows that the factorial of 5 is 120.
Frequently Asked Questions
Recursion is a programming technique in which a function calls itself repeatedly to solve a problem. The recursive function breaks down a problem into smaller and simpler sub-problems and then solves them using the same function.
Recursion has several advantages in programming:
Simplifies code, Reduces memory usage, Avoids repetition etc.
Recursion in factorial refers to a programming technique where a function calls itself to calculate the factorial of a given number. For example, to find the factorial of a number n, a function can be defined that calls itself with n-1 as the argument and multiplies the result by n until n reaches 1. This process continues until the base case is reached, and the final result is returned.
- #include<stdio.h>
- long factorial(int n)
- {
- if (n == 0)
- return 1;
- else.
- return(n * factorial(n-1));
- }
Here are some examples of problems that can be solved using recursion:
- Factorial calculation
- Fibonacci sequence
- Tower of Hanoi
- Binary search
- Merge sort
- Quick sort
- Tree traversal
There are two main types of recursion:
- Direct Recursion: In direct recursion, a function calls itself directly. This is the most common type of recursion.
- Indirect Recursion: In indirect recursion, a function calls another function which eventually calls the first function again. This creates a cycle of function calls.