What is recursion and how to write it?

In most simple words recursion is a âfunction calling itselfâ. Which can be an infinite number of times or some finite number of times. But at each step or operation, a function is calling itself.
What is the point of calling the same function in which the code is currently running or why a function needs to call itself?
In most programming languages we use a loop to do some work multiple number of times without writing the same code over and over again. But in some programming languages, there are no loop statements available at all like Haskell. So instead of a loop, they use recursion to perform some number of operations over and over again.
What my point here is, that recursion is just like a loop that runs the same code over and over again until some specific condition comes true and the recursion stop, just like in a loop.
The one main benefit of writing a recursion over a loop is, it's more readable than the loop statement.
How to write a recursion?
In the below coding example, we are calling a function âprintHelloâ, which on line 7 prints the word âhelloâ and then on line 8 calls itself which is going to perform the same operation over and over again â to an infinite number of times.
I used the word infinite, but in actuality, it wonât going to run and print âhelloâ forever because there is a limited amount of memory present in every computer. If it were a loop then using the term infinite would have been current but recursions donât work like a loop, yes they perform the same operation over and over again just like a loop but under the hood, they donât work like the loop.
How does it work then?
when any function calls itself, the calling function does not finish or vanish from the memory until the called function returns, and the same happens for the called function and so on. And the above code there is no break or a stop sign which can tell a recursion to stop and return. And thatâs why once the computer memory gets full with the recursion calls it stops the execution of the program and throws a stack overflow error/exception. Your computer uses something known as a stack to keep the track of recursion calls, which is nothing but a data structure.

How to tell a recursion to stop after it runs a certain number of times?
For that, we use something known as a base case which tells the recursion to stop and return.
In the below code we are again printing the hello, but this time it will only print it for 5 times.
Why, because as you can see on line 7 we have added a condition, and if that condition comes true we return Or we say printHello function to return where you come from, and then code which is on lines 10 and 11 wonât get executed for that particular call. And this condition that has the power to stop the recursion is called the base case. Now, this condition can be anything here it's just checking if the âtimesâ is equal to 5 if it is return else print the word hello and call the printHello function again with (times + 1) as an argument.
If you learned anything from this article please press that follow button
if (!already)đ