Presentation is loading. Please wait.

Presentation is loading. Please wait.

240-222 CPT: Functions/31 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to describe C functions –illustrate recursion.

Similar presentations


Presentation on theme: "240-222 CPT: Functions/31 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to describe C functions –illustrate recursion."— Presentation transcript:

1 240-222 CPT: Functions/31 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to describe C functions –illustrate recursion 3. Functions

2 240-222 CPT: Functions/32 Overview 1.Examples 2.Type Coercion 3.Call by Value 4.Scope Rules 5.Recursion 6.A Skeleton with Functions

3 240-222 CPT: Functions/33 1. Examples /* Using a square function (Fig. 5.3) */ #include int square(int y); int main() { int x; for (x = 1; x <= 10; x++) printf("%d ", square(x)); printf("\n"); return 0; } continued function prototype

4 240-222 CPT: Functions/34 int square(int y) /* returns the square of y */ { return y * y; } function definition

5 240-222 CPT: Functions/35 /* Finding the maximum of three integers (Fig. 5.4) */ #include int maximum(int x, int y, int z); int main() { int a, b, c; printf("Enter three integers: "); scanf("%d%d%d", &a, &b, &c); printf("Maximum is: %d\n", maximum(a, b, c)); return 0; } continued

6 240-222 CPT: Functions/36 int maximum(int x, int y, int z) /* returns the biggest of x, y and z */ { if (x > y && x > z) return x; else if (y > z) return y; else return z; }

7 240-222 CPT: Functions/37 Execution Enter three integers: 22 85 17 Maximum is 85

8 240-222 CPT: Functions/38 2. Type Coercion #include double sqroot(double n); int square(int y); int main() { printf("%f %f\n", sqroot(4.0), sqroot(4)); printf("%d %d\n", square(4), square(4.5)); return 0; } continued

9 240-222 CPT: Functions/39 double sqroot(double n) /* Use Newton Raphson to return the square root of n */ { float x0, eps = 0.000001; x0 = n; while ( abs(x0*x0 - n)/n > eps) x0 = (x0 + n/x0)/2; return x0; } int square(int y) { return y*y; }

10 240-222 CPT: Functions/310 Type Ordering (simplified) long double double float int char can be coerced to

11 240-222 CPT: Functions/311 Why Simplfied? Simplified in the sense that int can also be prefixed with the keywords: –short –long –unsigned l These refer to the size and range of the integer.

12 240-222 CPT: Functions/312 3. Call By Value #include int compute_sum(int x); int main() { int n = 3, sum; printf("%d\n",n); sum = compute_sum(n); printf("%d\n",n); printf("%d\n",sum); return 0; } continued

13 240-222 CPT: Functions/313 int compute_sum(int x) /* sum the integers from 1 to x */ { int tot = 0; for ( ; x > 0 ; --x) tot += x; printf("%d\n", x); return tot; }

14 240-222 CPT: Functions/314 4. Scope Rules l A block is a compound statement with declarations: { int x; s1; s2; }

15 240-222 CPT: Functions/315 l An identifier is accessible only within the block where it is declared. l The scope of an identifier is that part of the program where the identifier is accessible.

16 240-222 CPT: Functions/316 Examples int main() { int a = 2; printf("%d\n", a); { int a = 7; printf("%d\n", a); } printf("%d\n", ++a); return 0; }

17 240-222 CPT: Functions/317 A similar piece of code: int main() { int a_outer = 2; printf("%d\n", a_outer); { int a_inner = 7; printf("%d\n", a_inner); } printf("%d\n", ++a_outer); return 0; }

18 240-222 CPT: Functions/318 #include int compute_sum(int n); int main() { int n = 3, sum; printf("%d\n",n); sum = compute_sum(n); printf("%d\n",n); printf("%d\n",sum); return 0; } continued

19 240-222 CPT: Functions/319 int compute_sum(int n) /* sum the integers from 1 to n */ { int sum = 0; for ( ; n > 0 ; --n) sum += n; printf("%d\n", n); return sum; }

20 240-222 CPT: Functions/320 5. Recursion Sec. 5.13 5.1.Factorial 5.2.The Fibonacci Series 5.3.Depth Tester

21 240-222 CPT: Functions/321 5.1. Factorial l Mathematical Definition: fac n =1,if n <= 1 =n * fac (n - 1),otherwise

22 240-222 CPT: Functions/322 The C version: long int factorial(long int n) { if (n <= 1) return 1; else return (n * factorial(n - 1)); }

23 240-222 CPT: Functions/323 /* full program (fig. 5.14) */ #include long int factorial(long int n); int main() { int i; for (i = 1; i <= 10; i++) printf("%ld\n", factorial(i)); return 0; } continued

24 240-222 CPT: Functions/324 long int factorial(long int n) /* return the factorial of n */ { if (n <= 1) return 1; else return (n * factorial(n - 1)); }

25 240-222 CPT: Functions/325 Recursion Uses Lots of Memory fact(10) fact(9) fact(8) fact(1) 3628800 362880 40320 1

26 240-222 CPT: Functions/326 A Common Error long factorial(long n) { if (n <= 1) return 1; else return (n * factorial(--n)); }

27 240-222 CPT: Functions/327 5.2. The Fibonacci Series l 0, 1, 1, 2, 3, 5, 8, 13, 21,... l Mathematical definition: fib n = 0if n = 0 = 1if n = 1 = fib (n-1) + fib (n-2)if n > 1

28 240-222 CPT: Functions/328 The C version long int fib(long int n) { if (n == 0 || n == 1) return n; else return fib(n - 1) + fib(n - 2); }

29 240-222 CPT: Functions/329 /* full program (fig. 5.15) #include long int fib(long int n); int main() { long int result, number; printf("Enter an integer: "); scanf("%ld", &number); result = fib(number); printf("Fibonacci(%ld) = %ld\n", number, result); return 0; } continued

30 240-222 CPT: Functions/330 long int fib(long int n) /* return the nth fibonacci number */ { if (n == 0 || n == 1) return n; else return fib(n - 1) + fib(n - 2); }

31 240-222 CPT: Functions/331 Executions Enter an integer: 5 Fibonacci(5) = 5 Enter an integer: 20 Fibonacci(20) = 6765

32 240-222 CPT: Functions/332 Recursion can be Inefficient fib(5) fib(4)fib(3) fib(2) fib(1) fib(3) fib(2) fib(1)fib(0) 1 0 1 fib(1)fib(0) 10 fib(1)fib(2) fib(1)fib(0) 1 0 1 + + + ++ + +

33 240-222 CPT: Functions/333 5.3. Depth Tester /* Test the depth of recursion for sum() in steps of 100 */ #include long int sum(long int n); int main() { long int n = 0; for ( ; ; n +=100) printf("Recursion Test: n = %ld sum = %ld\n", n, sum(n)); return 0; }

34 240-222 CPT: Functions/334 long int sum(long int n) /* sum the integers between 1 and n */ { if ( n <= 1) return n; else return ( n + sum(n - 1)); }

35 240-222 CPT: Functions/335 Results on a PC : : Recursion Test: n = 7900 sum = 31208950 Recursion Test: n = 8000 sum = 32004000 Recursion Test: n = %ld sum = %ld > /* DOS prompt */

36 240-222 CPT: Functions/336 Execution Stack 32004000 31996001 31988003 sum(8001) sum(8000) sum(7999) sum(7998) sum(1) 1 ?

37 240-222 CPT: Functions/337 But on a workstation : Recursion Test: n = 65100 sum = 2119037550 Recursion Test: n = 65200 sum = 2125552600 Recursion Test: n = 65300 sum = 2132077650 Recursion Test: n = 65400 sum = 2138612700 Recursion Test: n = 65500 sum = 2145157750 Recursion Test: n = 65600 sum = -2143254496 Recursion Test: n = 65700 sum = -2136689446 Recursion Test: n = 65800 sum = -2130114396 Recursion Test: n = 65900 sum = -2123529346 Recursion Test: n = 66000 sum = -2116934296 Recursion Test: n = 66100 sum = -2110329246 : :

38 240-222 CPT: Functions/338 6. A Skeleton with Functions /* Author & program details */ #include int func1(int x); double func2(double y); /*... more prototypes */ int main() { /* declare variables */ /* do something with functions */ return 0; } continued

39 240-222 CPT: Functions/339 int func1(int x) /* some comments */ { /* do something */ return /* an integer */ ; } continued

40 240-222 CPT: Functions/340 double func2(double y) /* some comments */ { /* do something */ return /* a double */ ; } /*... more function definitions */


Download ppt "240-222 CPT: Functions/31 240-222 Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to describe C functions –illustrate recursion."

Similar presentations


Ads by Google