240-222 cpt: functions/31 240-222 computer programming techniques semester 1, 1998 objectives of...

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

Upload: clifton-dominic-campbell

Post on 04-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

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

240-222 CPT: Functions/3 1

240-222 Computer Programming Techniques240-222 Computer Programming TechniquesSemester 1, 1998Semester 1, 1998

Objectives of these slides:– to describe C functions

– illustrate recursion

3. Functions

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

240-222 CPT: Functions/3 2

OverviewOverview

1. Examples

2. Type Coercion

3. Call by Value

4. Scope Rules

5. Recursion

6. A Skeleton with Functions

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

240-222 CPT: Functions/3 3

1. Examples1. 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; }

continued

function prototype

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

240-222 CPT: Functions/3 4

int square(int y)/* returns the square of y */{ return y * y;}

function definition

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

240-222 CPT: Functions/3 5

/* 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

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

240-222 CPT: Functions/3 6

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;}

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

240-222 CPT: Functions/3 7

ExecutionExecution

Enter three integers: 22 85 17Maximum is 85

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

240-222 CPT: Functions/3 8

2. Type Coercion2. 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

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

240-222 CPT: Functions/3 9

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; }

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

240-222 CPT: Functions/3 10

Type Ordering (simplified)Type Ordering (simplified)

long double

double

float

int

char

can be coerced to

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

240-222 CPT: Functions/3 11

Why Simplfied?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.

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

240-222 CPT: Functions/3 12

3. Call By Value3. 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

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

240-222 CPT: Functions/3 13

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;}

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

240-222 CPT: Functions/3 14

4. Scope Rules4. Scope Rules

A block is a compound statement with declarations:

{ int x; s1; s2;}

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

240-222 CPT: Functions/3 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.

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

240-222 CPT: Functions/3 16

ExamplesExamples

int main(){ int a = 2; printf("%d\n", a);

{ int a = 7; printf("%d\n", a); }

printf("%d\n", ++a); return 0;}

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

240-222 CPT: Functions/3 17

A similar piece of code: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;}

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

240-222 CPT: Functions/3 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

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

240-222 CPT: Functions/3 19

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;}

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

240-222 CPT: Functions/3 20

5. Recursion 5. Recursion Sec. 5.13Sec. 5.13

5.1. Factorial

5.2. The Fibonacci Series

5.3. Depth Tester

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

240-222 CPT: Functions/3 21

5.1. Factorial5.1. Factorial

Mathematical Definition:

fac n= 1, if n <= 1= n * fac (n - 1),

otherwise

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

240-222 CPT: Functions/3 22

The C version:The C version:

long int factorial(long int n){ if (n <= 1) return 1; else return (n * factorial(n - 1));}

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

240-222 CPT: Functions/3 23

/* 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

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

240-222 CPT: Functions/3 24

long int factorial(long int n)/* return the factorial of n */{ if (n <= 1) return 1; else return (n * factorial(n - 1));}

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

240-222 CPT: Functions/3 25

Recursion Uses Lots of MemoryRecursion Uses Lots of Memory

fact(10)

fact(9)

fact(8)

fact(1)

3628800

362880

40320

1

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

240-222 CPT: Functions/3 26

A Common ErrorA Common Error

long factorial(long n){ if (n <= 1) return 1; else return (n * factorial(--n));}

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

240-222 CPT: Functions/3 27

5.2. The Fibonacci Series5.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

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

240-222 CPT: Functions/3 28

The C versionThe C version

long int fib(long int n){ if (n == 0 || n == 1) return n; else return fib(n - 1) + fib(n - 2);}

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

240-222 CPT: Functions/3 29

/* 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

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

240-222 CPT: Functions/3 30

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);}

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

240-222 CPT: Functions/3 31

ExecutionsExecutions

Enter an integer: 5Fibonacci(5) = 5

Enter an integer: 20Fibonacci(20) = 6765

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

240-222 CPT: Functions/3 32

Recursion can be InefficientRecursion 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)

1 0

fib(1)fib(2)

fib(1) fib(0)

1 0

1

+

+

+

+ +

+

+

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

240-222 CPT: Functions/3 33

5.3. Depth Tester5.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;}

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

240-222 CPT: Functions/3 34

long int sum(long int n)/* sum the integers between 1 and n */{ if ( n <= 1) return n; else return ( n + sum(n - 1));}

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

240-222 CPT: Functions/3 35

Results on a PCResults on a PC

::

Recursion Test: n = 7900 sum = 31208950Recursion Test: n = 8000 sum = 32004000Recursion Test: n = %ld sum = %ld> /* DOS prompt */

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

240-222 CPT: Functions/3 36

Execution StackExecution Stack

32004000

31996001

31988003

sum(8001)

sum(8000)

sum(7999)

sum(7998)

sum(1) 1

?

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

240-222 CPT: Functions/3 37

But on a workstationBut on a workstation

:Recursion Test: n = 65100 sum = 2119037550Recursion Test: n = 65200 sum = 2125552600Recursion Test: n = 65300 sum = 2132077650Recursion Test: n = 65400 sum = 2138612700Recursion Test: n = 65500 sum = 2145157750Recursion Test: n = 65600 sum = -2143254496Recursion Test: n = 65700 sum = -2136689446Recursion Test: n = 65800 sum = -2130114396Recursion Test: n = 65900 sum = -2123529346Recursion Test: n = 66000 sum = -2116934296Recursion Test: n = 66100 sum = -2110329246

::

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

240-222 CPT: Functions/3 38

6. A Skeleton with Functions6. 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

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

240-222 CPT: Functions/3 39

int func1(int x)/* some comments */{ /* do something */ return /* an integer */ ;}

continued

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

240-222 CPT: Functions/3 40

double func2(double y)/* some comments */{ /* do something */ return /* a double */ ;}

/* ... more function definitions */