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.
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.
– Maniero
In addition to the answers, I give the example of the mathematical factorial, which uses recursion (ref.:
(n+1)! = n! * (n+1)
)– vinibrsl