“In order to understand recursion, one must first understand recursion.”
📌Fun Activity
Google 'recursion' and you will find that Google will ask 'Did you mean: recursion'. Despite clicking on recursion from spell checker – Did you mean, Google will keep on displaying it. Though the spelling is right, Google keeps on telling it again and again.
Well, that is what exactly recursion is.
📌What is recursion?
Recursion is the process that comes into existence when a function calls a copy of itself to work on a smaller problem. Any function which calls itself is called a recursive function.
📌Example
Consider you need to find a way to your home. So you could define the operation "find your way home" as- 1. If you are at home, stop moving. 2. Take one step toward home. 3. "find your way home".
In the above example -
First, we don't go home if we are already home. Secondly, we do a very simple action that makes our situation simpler to solve. Finally, we redo the entire algorithm.
This is where the very last statement is calling the recursive algorithm.
→ RECURSION
📌Infinite recursion
Infinite Recursion occurs when the recursion carries on infinitely.
Example:
A recursive function always has to say when to stop repeating itself. Hence there should always be two parts to a recursive function: the recursive case and the base case.
📌Base case and the recursive case
The recursive case is when the function calls itself. The base case is when the function stops calling itself. This prevents infinite loops.
Example:
📌Factorial recursion example
You can code the factorial function easily using the recursion approach by using the formula
factorial(x)=x*factorial(x-1)
📌The call stack
Recursive functions use something called “the call stack.” When a program calls a function, that function goes on top of the call stack.
I will show you the call stack in action with the factorial function.
📌Illustration
Let’s see what happens if you call factorial(3). The illustration below shows how the stack changes, line by line.
The topmost box in the stack tells you what call to factorial you’re currently on.
Notice how each call to factorial has its own copy of x. This is very important to making recursion work. You can’t access a different function’s copy of x.
I hope you might have understood what recursion is. Thanks for getting to the end of the thread.
📌What are threads?
Threads allow a program to operate more efficiently by doing multiple things at the same time.
Every java application has at least one thread – the main thread. But from the application point of view – main is the first java thread and we can create multiple threads from it.
Multithreading refers to two or more threads executing concurrently in a single program
Many people ask me that 'Which programming language should I learn..?'
Well here are some programming languages with their uses. Choose yourself based on your interest.
📌Python -
🔸Artificial Intelligence
🔸Machine Learning
🔸Data analytics
🔸Data visualization
🔸Web development using the frameworks - Django, Pyramid, and flask
🔸Game development using python
🔸Python used in the field of Search Engine Optimisation (SEO)
📌What is inheritance?
When we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class (and avoid the writing of the same code again). This is referred to as inheritance.
As a programmer you might have come across both these words- coding and programming but do you know the actual difference between the two
Let us look at the difference between both these terms now and let me clear your doubts.
Read to the end of the thread!
📌Coding
Coding is basically the act of translating codes from human language to machine-based language. It can also be called a subset of programming since it is the foundation of programming