What is a recursive function?

I've always heard that recursion is a smart idea that plays a central role in functional programming and Computer Science in general. As I understand it, succinctly, There is mutual recursion and tail.

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

Author: viana, 2017-02-23

2 answers

A recursive function is a function that calls itself.

They can be used to be able to process a certain 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 is completed, the function is called immediately, but until next() has some return that causes the code of then to be triggered.

I had asked a question where the use of recursion is addressed, which although it is a little different, gives you an idea of the advantage in using it.

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

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

What is the difference between recursion and tail recursion?

 2
Author: Wallace Maxters, 2017-04-13 12:59:31

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

She's actually horrible, mostly for memory handling and 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 an algorithm, with recursion, called hanoi tower, made in C.

You may notice that she calls herself.

MUTUAL RECURSION

Mutual Recursion occurs when two or more functions they 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 statement to be executed.

 0
Author: gabrielfalieri, 2017-02-23 16:07:31