-
هي فانكشن بتنادي على نفسها
-
Useful when you want to solve a problem by dividing it into a smaller similar problems.
-
زيها زي الـ Loops لازم حاجة توقف التكرار اللي بيحصل
-
Each recursive code has 3 main parts:
- State (data saved for each call) بالتحديد البارمترز لكل كول
- Transition (to move from a state to another)
- Base case (to terminate the recursive calls) اللي بتقفل العملية دي
Fibonacci Series
The following example generates the Fibonacci series for a given number using a recursive function
#include <stdio.h>
int fibonacci(int i) {
if(i == 0) {
return 0;
}
if(i == 1) {
return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}
int main() {
int i;
for (i = 0; i < 10; i++) {
printf("%d\t\n", fibonacci(i));
}
return 0;
}
0
1
1
2
3
5
8
13
21
34