What is ‘Base Case’ in Recursion?
Last Updated :
06 Jan, 2024
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++
#include <bits/stdc++.h>
using namespace std;
void printFun( int test)
{
if (test < 1)
return ;
cout << test << " ";
printFun(test - 1);
cout << test << " ";
return ;
}
int main()
{
int test = 3;
printFun(test);
}
|
Java
public class RecursivePrint {
public static void printFun( int test) {
if (test < 1 ) {
return ;
}
System.out.print(test + " " );
printFun(test - 1 );
System.out.print(test + " " );
}
public static void main(String[] args) {
int test = 3 ;
printFun(test);
}
}
|
Python3
def printFun(test):
if test < 1 :
return
print (test, end = " " )
printFun(test - 1 )
print (test, end = " " )
if __name__ = = "__main__" :
test = 3
printFun(test)
|
C#
using System;
class GFG {
static void PrintFun( int test)
{
if (test < 1)
return ;
Console.Write(test + " " );
PrintFun(test - 1);
Console.Write(test + " " );
return ;
}
static void Main()
{
int test = 3;
PrintFun(test);
}
}
|
Javascript
function printFun(test) {
if (test < 1) {
return ;
}
console.log(test + ' ' );
printFun(test - 1);
console.log(test + ' ' );
}
function main() {
const test = 3;
printFun(test);
}
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++
#include <iostream>
using namespace std;
int fact( int n)
{
return n * fact(n - 1);
if (n <= 1)
return 1;
}
int main()
{
int n = 5;
cout << fact(n) << endl;
return 0;
}
|
Java
import java.io.*;
class Main {
static int fact( int n)
{
if (n <= 1 ) {
return 1 ;
}
return n * fact(n - 1 );
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(fact(n));
}
}
|
Python3
def fact(n):
if n < = 1 :
return 1
return n * fact(n - 1 )
n = 5
print (fact(n))
|
C#
using System;
class Program
{
static int Fact( int n)
{
if (n <= 1)
return 1;
return n * Fact(n - 1);
}
static void Main()
{
int n = 5;
Console.WriteLine(Fact(n));
}
}
|
Javascript
function GFG(n) {
if (n <= 1) {
return 1;
}
return n * GFG(n - 1);
}
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.
Share your thoughts in the comments
Please Login to comment...