660 likes | 762 Views
Recursion. Functions – reminder. return-type name( argType1 arg1 , argType2 arg2 , …) { function body; return value; }. A function can call other functions. Return causes the execution of the function to terminate and returns a value to the calling function.
E N D
Functions – reminder return-type name(argType1arg1,argType2arg2, …) { function body; return value; } • A function can call other functions. • Return causes the execution of the function to terminate and returns a value to the calling function. • The type of the value returned must be the same as the return-type defined for the function.
A recursive definition • C functions can also call themselves! • However, usually not with the same parameters (why?) • Some functions can be defined using smaller occurrences of themselves. • Such a definition is called a “recursive definition” of a function.
Recursive calling • Example: void func(int n){ putchar(`*`); func(n); } • Infinite series of * • What is the problem ? • Look for other problem …
(n-1)! Factorial • By definition : n! = 1*2*3*… *(n-1)*n • Thus, we can also define factorial the following way: • 0! = 1 • n! = n*(n-1)! for n>0 *n
Example - factorial intfactRec(int n){ if (n==0 || n==1) return 1; return n*factRec(n-1); } int factorial(int n){ int fact = 1; while (n >= 1){ fact *=n; n--; } return fact; }
Conclusions for Recursive calling • Every recursive function has a “boundary condition”. The function stops calling itself when it is satisfied.
Recursive factorial – step by step intfactRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns…
Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); } FactRec(4) n 4 Returns… 4*…
FactRec(4) FactRec(3) n n 4 3 Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) n n 4 3 Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) n n 4 3 Returns… Returns… 3*… Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… 2*… Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(1) FactRec(3) FactRec(2) n n n n 2 1 3 4 Returns… Returns… Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(1) FactRec(3) FactRec(2) n n n n 2 1 3 4 Returns… Returns… Returns… Returns… Recursive factorial – step by step intfactRec(int n) { if (n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) FactRec(2) FactRec(1) n n n n 2 3 4 1 Returns… Returns… Returns… Returns… 1 Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) FactRec(2) n n n 4 3 2 Returns… Returns… Returns… 2*1 Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) FactRec(3) n n 4 3 Returns… Returns… 3*2 Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }
FactRec(4) n 4 Returns… 4*6 Recursive factorial – step by step intfactRec(int n) { if(n==0 || n==1) return 1; return n*factRec(n-1); }
הדפסת מספרים- דוגמא 1 #include <stdio.h> void print1(int n){ if (n>=0){ printf("%d ",n); print1(n-1); { { void main(){ int i = 3; print1(i); putchar('\n'); { 3 2 1 0
הדפסת מספרים- דוגמא 2 #include <stdio.h> void print2(int n){ if (n>=0){ print2(n-1); printf("%d ",n); { { void main(){ int i = 3; print2(i); putchar('\n'); { 0 1 2 3
הדפסת מספרים- דוגמא 3 #include <stdio.h> void print3(int n){ if (n>=0){ printf("%d ",n); print3(n-1); printf("%d ",n); { { void main(){ int i = 3; print3(i); putchar('\n'); { 0 1 2 3 3 2 1 0
הדפסת מספרים- דוגמא 4 #include <stdio.h> void print4(int n){ if (n>=0){ print4(n-1); printf("%d ",n); print4(n-1); { { void main(){ int i = 3; print4(i); putchar('\n'); { 0 0 1 1 0 0 3 0 0 1 1 0 0 2 2
Another example - power • Xy= x*x*…*x • Recursive definitions (assume non-negative y): • Base: x0=1 • Xy= X*(Xy-1) • Xy=(Xy/2)2(for even y’s only) y times
Fibonacci Series • Fibonacci definition: • n0 = 0 • n1 = 1 • nn = nn-1 + nn-2 0 1 1 2 3 5 8 13 21 34 55 …
Fibonacci Iterative void fibonacci(int n) { int Fn, Fn1, Fn2, ind; Fn2 = 0 ; Fn1 = 1 ; Fn = 0 ; if ( n == 1 ) Fn = 1 ; for (ind=2 ; ind <= n ; ind++){ Fn = Fn1 + Fn2; Fn2 = Fn1; Fn1 = Fn; } printf("F(%d) = %d \n", n, Fn); }
fibonacci(5) fibonacci(4) fibonacci(3) fibonacci(3) fibonacci(2) fibonacci(2) fibonacci(1) fibonacci(2) fibonacci(1) fibonacci(1) fibonacci(0) fibonacci(1) fibonacci(0) fibonacci(1) fibonacci(0) Fibonacci Recursive int fibonacci(int n) { if (n==0) return 0; if (n==1) return 1; return fibonacci(n-1) + fibonacci(n-2); }
rec_pow(2, 5) x y 2 5 Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) x y 2 5 Returns… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) x y 2 5 Returns… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) x y 2 5 Returns… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) x y 2 5 Returns… 2*… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) rec_pow(2, 4) x x y y 2 2 5 4 Returns… Returns… rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) rec_pow(2, 4) x x y y 2 2 5 4 Returns… Returns… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) rec_pow(2, 4) x x y y 2 2 5 4 Returns… Returns… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) rec_pow(2, 4) x x y y 2 2 5 4 Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) rec_pow(2, 2) rec_pow(2, 4) x x x y y y 2 2 2 4 2 5 Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) rec_pow(2, 2) rec_pow(2, 4) x x x y y y 2 2 2 4 2 5 Returns… Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) rec_pow(2, 2) rec_pow(2, 4) x x x y y y 2 2 2 4 2 5 Returns… Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); }
rec_pow(2, 5) rec_pow(2, 4) x x y y 2 2 4 5 Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)
rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) x x x y y y 2 2 2 5 1 4 Returns… Returns… Returns… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)
rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) x x x y y y 2 2 2 5 1 4 Returns… Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)
rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) x x x y y y 2 2 2 5 1 4 Returns… Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)
rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) x x x y y y 2 2 2 5 1 4 Returns… Returns… Returns… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)
rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) x x x y y y 2 2 2 1 5 4 Returns… Returns… Returns… 2*… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)
rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) rec_pow(2, 0) x x x x y y y y 2 2 2 2 1 4 5 0 Returns… Returns… Returns… Returns… 2*… square(…) rec_pow – step by step int rec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)
rec_pow(2, 5) rec_pow(2, 1) rec_pow(2, 4) rec_pow(2, 0) x x x x y y y y 2 2 2 2 1 4 5 0 Returns… Returns… Returns… Returns… 2*… square(…) rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)
rec_pow(2, 4) rec_pow(2, 5) rec_pow(2, 0) rec_pow(2, 1) x x x x y y y y 2 2 2 2 5 4 0 1 Returns… Returns… Returns… Returns… square(…) 1 2*… rec_pow – step by step intrec_pow(int x, int y) { if (y == 0) return 1; if (y%2 == 0) return square(rec_pow(x,y/2)); else return x*rec_pow(x,y-1); } rec_pow(2, 2) x y 2 2 Returns… square(…)