Recursion

Recursion

The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using a recursive algorithm, certain problems can be solved quite easily. A recursive function solves a problem by calling a copy of itself and solving smaller subproblems of the original problems.It is essential to know that we should provide a certain case in order to terminate this recursion process.

When we call a function (or method) the process is as follows

  • Space is reserved on the stack for function parameters and their local variables.
  • The address of the code line from which the method was called is saved on the stack.
  • Function parameters and their values are stored on the stack
  • Finally, the allocated memory is released on the stack when the function ends and it returns to the original code call.

Instead, when the function is recursive. Each call generates a new function call with corresponding local objects. Re-executing completely, until the call itself. Where it recreates on the stack the new parameters and local variables. As many as recursive calls we generate. When finished, the memory is freed on the stack, starting from the last function created to the first, which will be the last to be freed.

One important thing to note is that every time a function is called from another function (even to itself), a new entry is created in the interpreter's call stack. This has a limited space so there may come a point where if you do too many it becomes saturated and an error occurs. This error is called "Stack Overflow".

Recursion example

An example that is easy to see and often used is the calculation of the factorial of an integer. The factorial of a number is defined as that number multiplied by the previous one, this number multiplied by the previous one, and so on until reaching 1. Thus, for example, the factorial of the number 5 would be: 5x4x3x2x1 = 120.

An example of this procedure in c would be this:

No hay texto alternativo para esta imagen

The same but with recursion and without using any loop could be done this way

No hay texto alternativo para esta imagen

Here I attach the link of a video where it is explained what happens behind this function:


An example with the factorial of 4:

No hay texto alternativo para esta imagen

Advantages and disadvantages of the recursion:

Advantages:

  • Solutions to complex problems in an easier, simpler, clearer and more elegant way.
  • It is not necessary to define the exact sequence of steps to solve the problem.
  • We can consider that "we have solved the problem" (smaller).
  • The efficiency of recursion lies in the fact that it can be used to solve problems that are difficult to solve iteratively.
  • Some problems are easier to implement using recursion.
  • It presents a facility to check and verify that the solution is correct.

Disadvantages:

  • inefficiency
  • Overhead associated with calls to subalgorithms.
  • A single call can generate a large number of recursive calls. (Fact(n) generates n recursive calls.)
  • Algorithm clarity may not compensate for algorithm overhead
  • Inefficient recursive algorithms.
  • It is necessary to create many variables which can cause problems in memory.
  • In general, a recursive function takes longer to generate than an iterative one.
  • Due to constant method calls, creation of dynamic variables on the stack, and duplication of variables.

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

Valentin Repetto的更多文章

  • Bun 1.0

    Bun 1.0

    La reciente llegada de Bun 1.0 ha causado revuelo en la comunidad de desarrolladores JavaScript.

  • Nest js

    Nest js

    Introduction I have been hearing about nest js for a long time now, about its potential and features but I had never…

  • TypeScript

    TypeScript

    What is Typescript? Typescript is a programming language that is transpiled into Javascript. It is a superset of…

  • What happens when you type google.com in your browser and press Enter

    What happens when you type google.com in your browser and press Enter

    Do you know what happens when you search for google.com in the browser? Internet is currently available to everyone…

  • Everything is an object in python

    Everything is an object in python

    Introduction Objects can be defined simply as the instance of a class that contains both data members and method…

  • Dynamic libraries

    Dynamic libraries

    To start talking about dynamic libraries, we will remember what libraries are and what they are used for. Certain types…

  • What happens when you type `ls -l *.c` in the shell?

    What happens when you type `ls -l *.c` in the shell?

    INTRODUCTION In this occasion, and as part of the “simple_shell” project, we have to write about the “ls -l *.c”…

  • Static libraries in c

    Static libraries in c

    This time I will inform you about the static libraries in c and how they work Whats are libreries? Libraries are a…

  • COMPILATION PROCESS

    COMPILATION PROCESS

    I will give a brief summary of how the compilation process works, from a high level language (that we can understand)…

社区洞察

其他会员也浏览了