Recursion Concept ,Implementation and Explanation: You need to know this

Reading Time: 3 minutes
recursion

Recursion is an important programming concept. It has a wide variety of uses especially while solving problems using data structures and implementing algorithms. If you are someone who is getting started with programming, or someone who is here for brushing up this concept, it will prove to be very helpful. In this article, we will look into the recursion concept, its implementation and necessary explanation. So lets get started.

What is Recursion?

Recursion is basically a function. Yes, it’s that simple. That’s why recursion is also termed as recursive function. But to be specific, recursion is the practice of calling a function within the same function. Sounds confusing? Just keep reading, everything will be clear. Well, we create functions in programming to break down complex tasks and call the necessary function when required. Recursive function is a function which calls itself. Recursive function facilitates repetitive computation or calculation. Every recursive function must have a base case (otherwise the program will be stuck in an infinite loop) and a recursive case which continues till the base case is reached. The concept will be clear with an example program.

C program to find factorial of a number using Recursion

#include <stdio.h>

// Function to calculate factorial using recursion
int factorial(int n) {
    if (n == 0) {
        return 1;  // Base case: 0! = 1
    } else {
        return n * factorial(n - 1);  // Recursive call
    }
}

int main() {
    int num;
    
    // Asking user for input
    printf("Enter a number to find its factorial: ");
    scanf("%d", &num);
    
    // Checking for non-negative input
    if (num < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        printf("Factorial of %d is %d\n", num, factorial(num));
    }
    
    return 0;
}

Explanation of the program

In the above program, the factorial() function is a recursive function, as you can see it is calling itself from within its code (line no. 8). Now the program runs in the following sequence:

  1. When the program is executed, the main() function runs and the user is asked to “enter a number to find its factorial”(line no. 16).
  2. User inputs a number (for explanation purpose, let us take it to be 3).
  3. The program checks for a negative input and if the input is negative it shows the message “Factorial is not defined for negative numbers”. Otherwise
  4. It prints the factorial of the number by calling the factorial() function and providing the number as parameter.
  5. In the factorial() function, at first the number is 3, so the function will return 3* factorial(3-1) i.e. 3* factorial(2).
  6. Again, the factorial(2) function will run and will return 2* factorial(1).
  7. This process goes on till the base case is reached. When the base case is reached, the final returned number is the multiplication of “3*2*1*1” which is the factorial of the number we provided i.e. the factorial of 3.

This is how recursion works, it needs a base case, without the base case, it runs infinitely and can cause the system to crash. The recursive case is executed till the base case is reached.

I hope you understood what recursion is and its application in programming. Thanks for reading the post. Do share it with others.

You may also like: Powerful programming languages you should learn in 2025

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
×