Recursion is not hard …..!

Yuvraj Agarkar
4 min readFeb 22, 2021

Hey everyone hope you’ll are having a productive day .Today I am gonna explain what on earth is Recursion …! Many new programmers try to avoid this concept just because they think that its hard but the fear is what killing you . I am gonna explain you with a very simple example of factorial program . for example factorial of 3 is 3*2*1 = 6 , prerequisites: basic knowledge of functions thats it …!

Let’s Go , so what is recursion the name itself tells us the meaning , Recursion is doing anything repeatedly until some condition is met , in Programming Recursion is when a function calls itself repeatedly until some end condition is met .Just like a loop but this is the last time when we are gonna compare it with loop (don’t get confused between loop and recursion). lets create a function called factorial .

#include <iostream>using namespace std;int factorial(int n){

if (n == 1){
return 1;
}
return n * factorial(n-1);
}
int main()
{
int n = 3, ans;
if (n < 0) {
cout<<"facotrial of negative program dosen't exits";
}else {
ans = factorial(n);
}

cout<<ans;
}

Here i have declared a variable n which equals to 3 and i have checked if it is <(smaller than) 0 i.e. negative number because factorial of negative number doesn’t exist we can’t calculate , so in else case we have called factorial function passing the n value which is 3 , take a close look at factorial function because this is where the magic happens , this function takes one input and return a integer value
Let’s deep dive in factorial function , first here we are checking if n == 1 because if it is than the factorial one will always be 1 we can simply return it , but what if we have n greater than 1 in this case we have n = 3 so than we return n * factorial(n-1) i.e. 3 * factorial(3–1) which is 3–1 = 2
here we are calling factorial function and passing n-1 so the value returned from factorial(n-1) will be multiplied with n , lets understand this with the help of a diagram below

Note:- number of steps will differ with different input value but the concept remains the same don’t learn anything try to understand

in step 1 we are calling factorial(n-1) which is factorial(3–1) so now that function will call itself and create a copy of it passing 3–1 i.e. 2

coming to step 2 we check if n==1 which is not, So in return statement we again call factorial(n-1) i.e factorial(2–1) which is 1 , So now that function will again call itself creating a copy of it passing 2–1 which is 1

coming towards step 3 , check if n==1 now yes indeed n equals to 1 we return 1 from this function
Note:- Once a function returns value it doesn’t check the code written after that return statement , in this case in step 3 when the condition evaluates to true it will return 1 and not check or bother about the statements written after that

The value returned in step 3 (which is 1) will be multiplied with 2 in step 2 so 2*1 is 2 now this value will be returned to step 1 ( from where this mayhem started) and multiplied with 3 so 3*2 = 6 and there is our answer , so this will be returned in our main function and assigned to the ans variable

yeaahh….! you have just experienced the journey of recursion , now try to read it again so you’ll have clear understanding that Recursion is not that difficult to understand and Do not skip this concept ever if you have to become a programmer or a developer in near future .

Now lets take some more in depth knowledge about it
1) When a condition is met that will end recursion in our case it was step 3 that case is called Base case and all the ones prior to that are called recursive case

2) Each time you call a function recursively it will create a copy of it eventually occupying double memory than previous one, extensive use of it can cause your memory to exhaust(finished or occupied completely)

3) Recursion helps in sub dividing a task of a function into smaller part. You can’t run a recursive function infinitely it has to stop a one point i.e. there should be an end condition

4) Recursion makes improves readability of your code but not always beneficial because as you saw it requires extra memory and can even exhaust it .In loops you don’t need any extra memory for each iteration so you can run a loop infinite times it wont exhaust the memory

OK , So that was it Not gonna overwhelm you but i hope you liked the Content . Feel free to DM and Follow me on Twitter .

--

--

Yuvraj Agarkar

Aspiring iOS developer , Like to make other developer’s life easy, giving my best to contribute in communities like these