What happens if Base Case is not Included or misplaced?

This placement position of Base Case is important because if we fail to mention the base case before the recursive call statement, the check wont happen and the recursion will go into infinity (till the memory is full).

Below is the C++ code snippit to elaborate the point:

C++




// C++ code for the above approach:
#include <iostream>
using namespace std;
int fact(int n)
{
 
    // Recursive Call (will get
    // called infintely)
    return n * fact(n - 1);
    // base case (this wont get executed ever)
    if (n <= 1)
        return 1;
}
 
// Drivers code
int main()
{
    int n = 5;
 
    // Function Call
    cout << fact(n) << endl;
    return 0;
}


Java




import java.io.*;
 
class Main {
    // Recursive function to calculate factorial
    static int fact(int n)
    {
        // Base case
        if (n <= 1) {
            return 1;
        }
 
        // Recursive call
        return n * fact(n - 1);
    }
 
    // Driver's code
    public static void main(String[] args)
    {
        int n = 5;
 
        // Function call
        System.out.println(fact(n));
    }
}


Python3




def fact(n):
    # Base case
    if n <= 1:
        return 1
     
    # Recursive call
    return n * fact(n - 1)
 
# Driver's code
n = 5
 
# Function Call
print(fact(n))


C#




using System;
 
class Program
{
    // Recursive function to calculate factorial
    static int Fact(int n)
    {
        // Base case
        if (n <= 1)
            return 1;
 
        // Recursive call
        return n * Fact(n - 1);
    }
 
    // Main function
    static void Main()
    {
        int n = 5;
 
        // Function call
        Console.WriteLine(Fact(n));
    }
}


Javascript




function GFG(n) {
    // Base case
    if (n <= 1) {
        return 1;
    }
    // Recursive call
    return n * GFG(n - 1);
}
// Driver's code
const n = 5;
console.log(GFG(n));


Note: Above code will run infinite times because it will never encounter the bases case thus the recursive calls will happen infinitely many times.

Summary:

A base case in recursion defines the stopping condition for the recursive function, ensuring that the recursion terminates when a specific condition is met. It plays a crucial role in breaking down complex problems into simpler ones and solving them step by step.



What is ‘Base Case’ in Recursion?

Base Case is defined as the condition in Recursive Function, which tells the function when to stop. It is the most important part of every Recursion, because if we fail to include this condition it will result in INFINITE RECURSIONS.

In the recursive program, the solution to the base case is provided and the solution to the bigger problem is expressed in terms of smaller problems.  

Similar Reads

Where is Base Case included in Recursion?

The Base Case can be inserted anywhere between the start of the Recursive Function and the recursive call statement....

What happens if Base Case is not Included or misplaced?

...