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 the Recursive Function and the recursive call statement.
Below is the C++ program to demonstrate the working of recursion:
// 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);
}
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);
}
}
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)
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);
}
}
// 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();
Output
3 2 1 1 2 3
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++ 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 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;
}
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));
}
}
def fact(n):
# Recursive call
return n * fact(n - 1)
# Base case (this won't get executed ever)
if n <= 1:
return 1
# Custom Input
n = 5
# Function Call
print(fact(n))
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));
}
}
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.