DEV Community

Cover image for Introduction to recursion (Part 1 Theory)
Abddelrahman Saad
Abddelrahman Saad

Posted on • Edited on

Introduction to recursion (Part 1 Theory)

Have you ever heard the term recursion function!?

if you're a developer - and you are of course - the answer would be definitely YES!

maybe this term makes you a little bit confused or maybe scared (as I'm at the first sight ) but please calm down it's really easy and very helpful.

We're going to divide this talk into two parts

Theory and Practical

Theory.

In theory, we need to answer 3 questions What, Why, and How.

WHAT?

ok, let's make it as simple as it indeed is.

Recursion is a "function calls itself".
Simply from the body of the function, we use the same function again over and over until reaches a certain result.

Yes, I've heard your question πŸ˜ƒ
why would I make a function to call itself again and what the benefits of doing so?

Here we go to the next question

WHY?

recursion as you already guessed (maybe not) is similar to loops.

But when you work with a large number of data (Objects, Numbers, etc.)
loops became a cumbersome process to our program and maybe break it, especially when you make two loops in the same process or loop inside a loop this consumes a lot of resources and slows down the process.

But we as developers always trying to make our code clean, fast, and as efficient as possible, Right?

In this situation recursion becomes handy.

we use it when we got a big number of data we need to loop inside it but you care about the time and readability and efficiency of your code, ok?

let's jump to the last question of theory

HOW?

here's how it works, there are two parts recursion function must have to be a recursion

1-Different input:
we agreed that it's a "function calls itself" so it doesn't make sense to give the function the same input and expect to do a different thing so every time the function executed must have different input.

2-Base case:
because recursion function kinda loop,
and every loop must have a time where it stops and returns a value, or we're going to have an infinite loop.
the time we stop our recursion function is the base case of the function.
so when we say the base case of the recursion function it means the phase that stops and returns its result.

That's it for theory, I hope it was simple, and follow me for more ❀️

The practical part here

https://dev.to/abd_elrahmanmo/introduction-to-recursion-practical-2odm]

Top comments (9)

Collapse
 
jenueldev profile image
Jenuel Oras Ganawed • Edited

Is rexursion something looks like this
Function add(number) {
return number <= 10? add(number++) : 'greater than 10'
}

Collapse
 
abdelrahman profile image
Abddelrahman Saad

Not exactly, as mentioned before recursion is something we often use it to loop inside a big number of items but in a cleaner and efficient way ❀️

Collapse
 
jenueldev profile image
Jenuel Oras Ganawed

Can u give a code example thanks πŸ˜ŠπŸ‘

Thread Thread
 
abdelrahman profile image
Abddelrahman Saad

Sure, in a couple of hours the next part will be available with the code example, I'll send the link to you ❀️

Thread Thread
 
abdelrahman profile image
Abddelrahman Saad

Hey, how are you, here's the next part I hope you like it❀️
dev.to/abd_elrahmanmo/introduction...

Collapse
 
jameshubert_com profile image
James Hubert

Ah well done covering this topic. It was just the hardest thing to learn the first time I tried digging into it.

Collapse
 
abdelrahman profile image
Abddelrahman Saad

I'm really glad to hear that, thanks a lot ❀️

Collapse
 
thedvlpr profile image
Sophie

Oh wow πŸ˜„ very well πŸ‘ where is the practical part?

Collapse
 
abdelrahman profile image
Abddelrahman Saad

Thank u for your feedback ❀️, it will be available tomorrow