Skip to main content

Command Palette

Search for a command to run...

Recursion: A Beginner's guide to avoid StackOverflow

Yeah, yeah I can get it recursion can be really overwhelming, sometimes it can feel like it will damage ur brain. Let me help u to avoid it

Published
•3 min read•View as Markdown
Recursion: A Beginner's guide to avoid StackOverflow
S
I write about Android dev & Open Source

Let's start with the basics:

If u search in google what is recursion, it will tell u something like a - " recursion is a method of solving a computational problem where the solution depends on solutions blah blah blah ... "

In simple terms, recursion is a loop that runs until the condition becomes true

That's the simplest explanation... So u may ask where should we put this loop, is this related to for or while loop ... Nah, it's just a function calling itself again and again ...

Let me explain :

// just a pseudo code

public void main (){
    // main method

     int i=0;

// calling fun
  fun(i);
}

public int fun(int i){

return (fun(i+1));
// the function calling itself again
}

so what's happening is this code is, let me show u with an example:

// main function will call ----> 'fun(0)'

// then 'fun(0)' will call ----> fun(i+1) i.e 'fun(1)'

// then 'fun(1)' will call -----> fun(i+1) i.e 'fun(2)'

// then 'fun(2)' will call ------> fun(i+1) i.e 'fun(3)'

Unfortunately, ur code will end up in a stack overflow error, why???, because this function now works as a loop, so u need to add a condition to stop the loop so...

public void main (){
    // main method

     int i=0;

// calling fun
  fun(i);
}

public int fun(int i){
if(i==4){
return 0;
// adding an if statement so that we can end the loop
}

return (fun(i+1));
// the function calls itself again
}


}

so what's happening here :

// main function will call ----> 'fun(0)'

// then 'fun(0)' will call ----> fun(i+1) i.e 'fun(1)'

// then 'fun(1)' will call -----> fun(i+1) i.e 'fun(2)'

// then 'fun(2)' will call ------> fun(i+1) i.e 'fun(3)'

// then 'fun(3)' will call ------> fun(i+1) i.e 'fun(4)'

// 'fun(4)' ------> return 0;

ooh!!! we reached the climax of our story: here, what was our condition, let me ask u again what was our condition ----> if(i==4){ return 0;}

this means if i ==0 then end our loop, so in short, by adding appropriate if conditions we can get our desired results

these conditions are known as ' base conditions ' in the programming world

Solving our 1st recursion question:

Q - Print the first 5 natural numbers using recursion ...(Try to do it first, without seeing the solution that's how u learn)

public void main (){

     int i=1;
     fun(i);
}

public void fun(int i){
if(i==6){
return ;
// when we reach our base condition we will return 
}

printf(i);
return (fun(i+1));


}

}

Output:

1

2

3

4

5

And that's it .... TaDaAaaaaa!!!

Final thought

Yeah, yeah I know it was a short blog as it was my blog, I wanted to keep it short and simple, I don't if people will like this blog or not so that's also the reason I kept it short, If u guys liked this blog, LIKE, SHARE AND SUBSCRIBE...

If u guys like it just comment I will write part 2 of this blog where we go into many details