What is 'Base Case' in Recursion? (original) (raw)
Last Updated : 13 Feb, 2026
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.
Where is Base Case included in Recursion?
The Base Case can be inserted anywhere between the start of th**e Recursive Function and the recursive call statement.
Below is the C++ program to demonstrate the working of recursion:
C++ `
// C++ code for the above approach: #include <bits/stdc++.h> using namespace std;
void printFun(int test) {
// Base Case
if (test < 1)
return;
cout << test << " ";
// Recursive Call Statement
printFun(test - 1);
cout << test << " ";
return;}
// Drivers code int main() { int test = 3;
// Function Call
printFun(test);}
Java
public class RecursivePrint { public static void printFun(int test) { // Base Case if (test < 1) { return; }
System.out.print(test + " ");
// Recursive Call Statement
printFun(test - 1);
System.out.print(test + " ");
}
public static void main(String[] args) {
int test = 3;
// Function Call
printFun(test);
}}
Python
def printFun(test): # Base Case if test < 1: return
print(test, end=" ")
# Recursive Call Statement
printFun(test - 1)
print(test, end=" ")Driver code
if name == "main": test = 3
# Function Call
printFun(test)C#
using System;
class GFG { // Function to print numbers from 1 to test and then // from test to 1 static void PrintFun(int test) { // Base Case if (test < 1) return;
// Print the current number
Console.Write(test + " ");
// Recursive Call Statement
PrintFun(test - 1);
// Print the current number again after the
// recursive call
Console.Write(test + " ");
return;
}
// Driver code
static void Main()
{
int test = 3;
// Function Call
PrintFun(test);
}}
JavaScript
// Function to recursively print numbers from test to 1 and back to test function printFun(test) { // Base Case if (test < 1) { return; }
// Print the current number
console.log(test + ' ');
// Recursive Call Statement
printFun(test - 1);
// Print the current number again when returning from the recursion
console.log(test + ' ');}
// Main function function main() { const test = 3;
// Function Call
printFun(test);}
// Invoke the main function to start the program main();
`
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 using namespace std; int fact(int n) { // Recursive Call (will get // called infintely) return n * fact(n - 1);
// base case (this won't 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) { // Recursive call return n * fact(n - 1);
// Base case (this won't get executed ever)
if (n <= 1) {
return 1;
}
}
// Driver's code
public static void main(String[] args)
{
int n = 5;
// Function call
System.out.println(fact(n));
}}
Python
def fact(n):
# Recursive call
return n * fact(n - 1)
# Base case (this won't get executed ever)
if n <= 1:
return 1Custom Input
n = 5
Function Call
print(fact(n))
C#
using System;
class Program { // Recursive function to calculate factorial static int Fact(int n) { // Recursive call return n * Fact(n - 1);
// Base case (this won't get executed ever)
if (n <= 1)
return 1;
}
// Main function
static void Main()
{
int n = 5;
// Function call
Console.WriteLine(Fact(n));
}}
JavaScript
function GFG(n) {
// Recursive call
return n * GFG(n - 1);
// Base case (this won't get executed ever)
if (n <= 1) {
return 1;
}}
// Custom Input const n = 5; console.log(GFG(n));
`
**Note: Above code will run infinite times because it will never encounter the base 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.