+ 3
thought for the day: to understand the concept of recursion, you must first understand what is recursion ;-)
2 Respostas
0
It's just when a function or method calls itself.
For example: factorial function
Say you were to enter a 3 as the parameter, it would return 1 * 2 * 3. Or if you enter 1 or 0, returns 1. And if the parameter doesn't have a factorial it returns -1.
long long factorial(long long num) {
switch (num) {
case 0:
case 1:
return 1;
default:
if (num > 0)
return factorial(num - 1) * num;
return -1;
}
}
0
Some simple example of reccursive function calcutating N fibonacci's number:
int fib(int n)
{
if(n==1 || n==2) return n;
return fib(n-1)+fib(n-2)
}