What is a recursive function?

Asked

Viewed 2,487 times

6

I’ve always heard that recursion is an intelligent idea that plays a central role in functional programming and computer science in general. From what I understand, briefly, there is recursion mutual and tail.

What is a recursive function? What are the main characteristics between mutual and tail recursiveness?

  • 1

    Besides the obvious that already has in other questions, I think that would only fit a more academic answer, so I’ll let other people answer.

  • In addition to the answers, I give the example of the mathematical factorial, which uses recursion (ref.: (n+1)! = n! * (n+1))

2 answers

4


A recursive function is a function that calls itself.

They can be used to process a given operation and there are usually internal conditions for recursivities to be applied (since without conditions, it would call itself eternally, causing what we call an infinite loop).

For example, an excerpt from my library for lazy ajax loading by Angular (Javascript framework):

WMLaravelLazyLoad.prototype.untilCompleted = function () {

    var that = this;

    that.next().then(function () {
        that.untilCompleted();
    })
};

In the above example, when the action is completed, the function is called immediately, but until next() has some feedback that causes the code of then be triggered.

I had asked a question where the use of recursion is approached, which although it is a little different, you can have an idea of the advantage of using it.

Why do they say that recursive setTimeout is better than setInterval?

On the question about mutual and tail recursiveness, you can see:

What is the difference between recursion and tail recursion?

1

The function is recursive is a function that calls itself, always.

It is actually horrible, mainly for treatment and memory management.

EXAMPLE OF RECURSIVE FUNCTION

#include <stdio.h>

void movetorre (int n, char orig, char dest, char aux){
   if (n==1) {printf("\nMover disco 1 da torre %c para a torre %c", orig, dest);
   return;}
      movetorre(n-1,orig,aux,dest);
      printf("\nMover disco %d da torre %c para a torre %c", n, orig, dest);
      movetorre(n-1,aux,dest,orig);
};

int main(){
   int discos;
   printf("\t\t\t\tTORRE DE HANOY\n\n");
   printf("Digite a quantidade de discos: ");
   scanf("%d",&discos);
   movetorre(discos,'A','C','B');
   return 0;
}

This is a simple example of a recursive algorithm called the Hanoi tower, made in c.

You may notice that she calls herself.

MUTUAL RECURSION

Mutual recursion occurs when two or more functions are defined in terms of each other.

The most important basic example of a data type that can be defined by mutual recursion is a tree, which can be mutually defined recursively in terms of a forest (a list of trees).

TAIL RECURSION

Tail recursive functions form a subclass of recursive functions, in which the recursive call is the last instruction to be executed.

  • "It is actually horrible". That’s where matters of opinion come in. It would be better to formulate pros and cons.

  • Yeah, she’s awful, every time you call her, she stores a piece of data in her memory, where to pop, it’s a bag. Analyzing for example, the algorithm, Hanoi tower, it is recursive and becomes the n... ie, has very easy memory burst

  • Are you talking specifically about the C language? You have to pay attention because the AP is asking about the concept in a general scope.

  • No, I’m talking about data structure itself

  • Hmmmmm. I don’t get it. What do you mean 'unstuck' is a @gabrielfalieri bag? This is transparent to you, however, I agree, of course, that depending on the available HEAP memory and the amount of times the recursion is executed the memory will not be sufficient.

  • Reginaldo Rigo, when you call the function, it goes saving in memory, it goes saving in memory, it goes saving in memory, it goes saving in memory, after it pops, when it reaches the desired result. This generates memory overflow

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.