What is recursion?

A thread🧵 Image
“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. Image
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: Image
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: Image
📌Factorial recursion example
You can code the factorial function easily using the recursion approach by using the formula
factorial(x)=x*factorial(x-1) Image
📌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. Image
Image
Image
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. Image
If you liked this, make sure to:

1. Follow me @meetjain74

2. Retweet the first tweet

3. Turn on the notifications to never miss these amazing tweets.

Let me know your thoughts below!!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Meet Jain🦅

Meet Jain🦅 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @meetjain74

8 Jan
Programming concepts through memes😜

A thread🧵
📌Recursion Image
📌Doubly circular linked list Image
Read 13 tweets
8 Jan
Threads in Java

A 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
Read 16 tweets
7 Jan
How to choose a programming language?

A thread🧵
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)
Read 9 tweets
4 Jan
Learning Data structures through diagrams

A thread🧵
🔸Arrays
🔸Stack
Read 10 tweets
3 Jan
Inheritance in Java

A thread🧵
📌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.
📌Example Image
Read 7 tweets
29 Dec 21
Coding vs Programming

A thread🧵
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
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(