Recursion in function use
1
We used the proof for the case n-1 to prove the case n
Factorial
2
int fact(int a){
if(a == 0) return 1;
return a * fact(a - 1);
}
int main(){
printf("%d", fact(1+1));
}
main()
fact()
a
fact()
fact()
a
a
2
2
1
1
0
0
1
1
2
2
2
1
1
Recap – 6 basic rules of C functions
3
Output
4
#include<stdio.h>
int recursive(int i) {
static int count = 0;
count = count + i;
return count;
}
int main() {
int i, j;
for (i = 0; i <= 5; i++)
j = recursive(i);
printf("%d\n", j);
return 0;
}