Download presentation
Presentation is loading. Please wait.
Published byChastity Rodgers Modified over 6 years ago
1
240-222 Computer Programming Techniques Semester 1, 1998
3. Functions Objectives of these slides: to describe C functions illustrate recursion
2
Overview 1. Examples 2. Type Coercion 3. Call by Value 4. Scope Rules
5. Recursion 6. A Skeleton with Functions
3
1. Examples /* Using a square function (Fig. 5.3) */ #include <stdio.h> int square(int y); int main() { int x; for (x = 1; x <= 10; x++) printf("%d ", square(x)); printf("\n"); return 0; } function prototype continued
4
function definition int square(int y) /* returns the square of y */ { return y * y; }
5
/. Finding the maximum of three integers (Fig. 5. 4)
/* Finding the maximum of three integers (Fig. 5.4) */ #include <stdio.h> 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
int maximum(int x, int y, int z) /. returns the biggest of x, y and z
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
Execution Enter three integers: Maximum is 85
8
2. Type Coercion #include <stdio.h> 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
double sqroot(double n) /
double sqroot(double n) /* Use Newton Raphson to return the square root of n */ { float x0, eps = ; x0 = n; while ( abs(x0*x0 - n)/n > eps) x0 = (x0 + n/x0)/2; return x0; } int square(int y) { return y*y; }
10
Type Ordering (simplified)
long double double can be coerced to float int char
11
Why Simplfied? Simplified in the sense that int can also be prefixed with the keywords: short long unsigned These refer to the size and range of the integer.
12
3. Call By Value #include <stdio.h> 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
int compute_sum(int x) /. sum the integers from 1 to x
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
4. Scope Rules A block is a compound statement with declarations:
{ int x; s1; s2; }
15
An identifier is accessible only within the block where it is declared.
The scope of an identifier is that part of the program where the identifier is accessible.
16
Examples int main() { int a = 2; printf("%d\n", a); { int a = 7; printf("%d\n", a); } printf("%d\n", ++a); return 0; }
17
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
#include <stdio.h> 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
int compute_sum(int n) /. sum the integers from 1 to n
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
5. Recursion Sec. 5.13 5.1. Factorial 5.2. The Fibonacci Series
5.3. Depth Tester
21
5.1. Factorial Mathematical Definition:
fac n = 1, if n <= 1 = n * fac (n - 1), otherwise
22
The C version: long int factorial(long int n) { if (n <= 1) return 1; else return (n * factorial(n - 1)); }
23
/. full program (fig. 5. 14). / #include <stdio
/* full program (fig. 5.14) */ #include <stdio.h> 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
long int factorial(long int n) /. return the factorial of n
long int factorial(long int n) /* return the factorial of n */ { if (n <= 1) return 1; else return (n * factorial(n - 1)); }
25
Recursion Uses Lots of Memory
fact(10) fact(9) 362880 fact(8) 40320 fact(1) 1
26
A Common Error long factorial(long n) { if (n <= 1) return 1; else return (n * factorial(--n)); }
27
5.2. The Fibonacci Series 0, 1, 1, 2, 3, 5, 8, 13, 21, . . . Mathematical definition: fib n = 0 if n = 0 = 1 if n = 1 = fib (n-1) + fib (n-2) if n > 1
28
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
/. full program (fig. 5. 15) #include <stdio
/* full program (fig. 5.15) #include <stdio.h> 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
long int fib(long int n) /. return the nth fibonacci number
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
Executions Enter an integer: 5 Fibonacci(5) = 5
32
Recursion can be Inefficient
fib(5) fib(4) + fib(3) fib(3) + fib(2) fib(2) + fib(1) fib(2) + fib(1) 1 fib(1) + fib(0) fib(1) + fib(0) 1 1 1 fib(1) + fib(0) 1
33
5.3. Depth Tester /* Test the depth of recursion for sum() in steps of 100 */ #include <stdio.h> 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
long int sum(long int n) /. sum the integers between 1 and n
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
Results on a PC : : Recursion Test: n = 7900 sum = Recursion Test: n = 8000 sum = Recursion Test: n = %ld sum = %ld > /* DOS prompt */
36
Execution Stack sum(8001) ? sum(8000) 32004000 sum(7999) 31996001
1 sum(1)
37
But on a workstation : Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = Recursion Test: n = sum = : :
38
6. A Skeleton with Functions
/* Author & program details */ #include <stdio.h> int func1(int x); double func2(double y); /* ... more prototypes */ int main() { /* declare variables */ /* do something with functions */ return 0; } continued
39
int func1(int x) /. some comments. / { /. do something. / return /
int func1(int x) /* some comments */ { /* do something */ return /* an integer */ ; } continued
40
double func2(double y) /. some comments. / { /. do something
double func2(double y) /* some comments */ { /* do something */ return /* a double */ ; } /* ... more function definitions */
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.