A classical example of recursions is of course the factorial one.
If you want to know the result of a factorial, do not hesitate to use a recursive function.
Let's see it:
#include <stdio.h> int doIt(int nb) { if (nb <= 1) return 1; printf("nb = %d\n", nb); return nb * doIt(nb - 1); } int main() { int first = 5; printf("Last = %d\n", doIt(first)); return 0; }
The render:
nb = 5 nb = 4 nb = 3 nb = 2 Final result = 120
© Badprog - I want to change the world. And I will.
Add new comment