Recursion

Recursion


No alt text provided for this image

First, what is it?

Recursion is a broad concept, complicated to think about, although we can see it in daily activities and, being aware of that, understand it more and more, for example, a photo of a photo, a live broadcast into a live broadcast among other situations.

A good example is that you imagine yourself waiting in a supermarket queue and you want to know how many people are in front of you without getting out of line and counting them, you can ask the person in front of you until someone knows how to answer and the answer comes back to you.

The concept we want to reach is programming-focused recursion: it allows defining an object (problems, data structures) in terms of itself.

Some problems defined recursively are the factorial of a number, the Fibonacci series, etc.

to remember the factorial of a number is the product of all the natural numbers before it.

in code:

No alt text provided for this image

Solving a problem using recursion means that the solution depends on the solutions of small instances of the same problem.

The key points to developing a recursive function are:

1- Each recursive call (to the same function) must define a less complex problem.

2 -There must be at least one base case or final condition (or it will be infinite).

if we identify this in the case of the factorial number the base case of the function is when we get to 0 without this condition the function would call itself infinitely.

Why use recursion and why not?

The first clear advantage is the elegance that it offers in the face of the complexity of a problem, that is, it reduces a complex problem to three or four lines of code.

The main advantages of recursion are optimization and reducing time complexity.

and drawbacks related to memory usage, as the number of method invocations in your computer's memory increases, you may run out of memory.

Now, another bad point, just like any piece of code, is that if recursion is overused, you can get this kind of habit where you start developing really complex code that is hard for outsiders to understand.

The questions we should ask ourselves to avoid this are:

  • Is this a good use case?
  • Can I really break it down into subproblems that make sense for recursion?

If it can't, we need to identify when we have unnecessarily complex code.

I hope I was able to be clear with the explanation of this concept, thanks for reading :)

要查看或添加评论,请登录

Santiago Neira的更多文章

  • Have you ever asked yourself what's going on behind your searches?

    Have you ever asked yourself what's going on behind your searches?

    Have you ever wondered what happens when you search for something in your browser? probably you answer will be no…

  • Have you heard about the IOT?

    Have you heard about the IOT?

    First question, what is the Internet of things ? in symply words is the concept of connecting any device that you can…

  • Mutables and Immutables Types..

    Mutables and Immutables Types..

    Python is an object-oriented programming language, in python, everything is an object. Let's see what this means: We…

  • Dynamic and static libraries in C

    Dynamic and static libraries in C

    First of all, what are bookstores? We must first ask ourselves what a programming library is, it is a type of file that…

    1 条评论
  • Do you know anything about static libraries in C ?

    Do you know anything about static libraries in C ?

    In this opportunity we have as a main topic the static libraries in C we are going to talk about topics such as: Why…

    2 条评论
  • Behind the compilation in C Language

    Behind the compilation in C Language

    This time in the blog we will focus on learning a little more about the compilation process of this programming…

社区洞察

其他会员也浏览了