What is a recursive function?

4

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, succinctly, there is mutual recursion and tail .

What is a recursive function? What are the main features between mutual recursion and tail?

    
asked by anonymous 23.02.2017 / 16:59

2 answers

2

A recursive function is a function that calls itself.

They can be used to process a particular operation, and there are usually internal conditions for recursion 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 loading of ajax by Angular (framework for Javascript):

WMLaravelLazyLoad.prototype.untilCompleted = function () {

    var that = this;

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

In the example above, when the action completes, the function is called immediately, but until next() has some return that causes the then code to fire.

I had asked a question where the use of recursion is addressed, although it is a bit different, you can see the advantage of using it.

Why say recursion of setTimeout is better than setInterval?

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

What is the difference between recursion and tail recursion?

    
23.02.2017 / 17:02
0

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

It's actually horrible, especially for memory handling and management.

RECURSIVE FUNCTION EXAMPLE

#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 an algorithm, with recursion, called the hanoi tower, made in c.

You can notice that she calls herself.

MUTUAL RESOURCES

Mutual recursion occurs when two or more functions are defined in terms of one another.

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 RESOURCES

The recursive functions in the tail form a subclass of the recursive functions, in which the recursive call is the last instruction to execute.

    
23.02.2017 / 17:07