Chapitre8 Fonctions
Chapitre8 Fonctions
com
Chapter 8 - Functions
Outline
1. Introduction
2. Program Modules in C
3. Math Library Functions
4. Functions
5. Header Files
6. Calling Functions: Call by Value and Call by Reference
7. Recursion
8. Recursion vs. Iteration
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
Objectives
www.almohandiss.com of 9.
www.almohandiss.com
Introduction
• Divide and conquer
– Construct a program from smaller pieces or components
• These smaller pieces are called modules
– Each piece more manageable than the original program
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
F u n c t io n D e s c r ip t io n E x a m p le
sqrt( x ) sq u a re ro o t o f x sqrt( 900.0 ) is 30.0
sqrt( 9.0 ) is 3.0
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
Function Definitions
• Function definition format
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
1 /* Fig. 5.3: fig05_03.c
Creating and using a programmer-defined function */
2
Outline
3 #include <stdio.h>
4
5 int square( int y ); /* function prototype */
6
7 /* function main begins program execution */
8 int main()
9 {
10 int x; /* counter */
11
12 /* loop 10 times and calculate and output square of x each time */
13 for ( x = 1; x <= 10; x++ ) {
14 printf( "%d ", square( x ) ); /* function call */
15 } /* end for */
16
17 printf( "\n" );
18
19 return 0; /* indicates successful termination */
20
21 } /* end main */
22
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
23 /* square function definition returns square of an integer */
24 int square( int y ) /* y is a copy of argument to function */
Outline
25 {
26 return y * y; /* returns square of y as an int */
27 fig05_03.c (Part 2
28 } /* end function square */ of 2)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
1 /* Fig. 5.4: fig05_04.c
Finding the maximum of three integers */
2
Outline
3 #include <stdio.h>
4
5 int maximum( int x, int y, int z ); /* function prototype */
6
7 /* function main begins program execution */
8 int main()
9 {
10 int number1; /* first integer */
11 int number2; /* second integer */
12 int number3; /* third integer */
13
14 printf( "Enter three integers: " );
15 scanf( "%d%d%d", &number1, &number2, &number3 );
16
17 /* number1, number2 and number3 are arguments
18 to the maximum function call */
19 printf( "Maximum is: %d\n", maximum( number1, number2, number3 ) );
20
21 return 0; /* indicates successful termination */
22
23 } /* end main */
24
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
25 /* Function maximum definition */
26 /* x, y and z are parameters */
Outline
27 int maximum( int x, int y, int z )
28 {
29 int max = x; /* assume x is largest */
30
31 if ( y > max ) { /* if y is larger than max, assign y to max */
32 max = y;
33 } /* end if */
34
35 if ( z > max ) { /* if z is larger than max, assign z to max */
36 max = z;
37 } /* end if */
38
39 return max; /* max is largest value */
40
41 } /* end function maximum */
Enter three integers: 22 85 17
Maximum is: 85
Enter three integers: 85 22 17
Maximum is: 85
Enter three integers: 22 17 85
Maximum is: 85
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
Function Prototypes
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
Header Files
• Header files
– Contain function prototypes for library functions
– <stdlib.h> , <math.h> , etc
– Load with #include <filename>
#include <math.h>
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
Recursion
• Recursive functions
– Functions that call themselves
– Can only solve a base case
– Divide a problem up into
• What it can do
• What it cannot do
– What it cannot do resembles original problem
– The function launches a new copy of itself (recursion step)
to solve what it cannot do
– Eventually base case gets solved
• Gets plugged in, works its way up and solves whole problem
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
Recursion
• Example: factorials
– 5! = 5 * 4 * 3 * 2 * 1
– Notice that
• 5! = 5 * 4!
• 4! = 4 * 3! ...
– Can compute factorials recursively
– Solve base case (1! = 0! = 1) then plug in
• 2! = 2 * 1! = 2 * 1 = 2;
• 3! = 3 * 2! = 3 * 2 = 6;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
5.13 Recursion
Fin a l va lue = 120
5! 5!
5! = 5 * 24 = 120 is re turn ed
5 * 4! 5 * 4!
4! = 4 * 6 = 24 is re turne d
4 * 3! 4 * 3!
3! = 3 * 2 = 6 is re tu rn e d
3 * 2! 3 * 2!
2! = 2 * 1 = 2 is re turne d
2 * 1! 2 * 1!
1 re turne d
1 1
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
1 /* Fig. 5.14: fig05_14.c
2 Recursive factorial function */ Outline
3 #include <stdio.h>
4
5 long factorial( long number ); /* function prototype */
6
7 /* function main begins program execution */
8 int main()
9 {
10 int i; /* counter */
11
12 /* loop 10 times. During each iteration, calculate
13 factorial( i ) and display result */
14 for ( i = 1; i <= 10; i++ ) {
15 printf( "%2d! = %ld\n", i, factorial( i ) );
16 } /* end for */
17
18 return 0; /* indicates successful termination */
19
20 } /* end main */
21
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
22 /* recursive definition of function factorial */
23 long factorial( long number )
Outline
24 {
25 /* base case */
26 if ( number <= 1 ) {
27 return 1;
28 } /* end if */
29 else { /* recursive step */
30 return ( number * factorial( number - 1 ) );
31 } /* end else */
32
33 } /* end function factorial */
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
return f( 2 ) + f( 1 )
return f( 1 ) + f( 0 ) return 1
return 1 return 0
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
1 /* Fig. 5.15: fig05_15.c
Recursive fibonacci function */
2
Outline
3 #include <stdio.h>
4
5 long fibonacci( long n ); /* function prototype */
6
7 /* function main begins program execution */
8 int main()
9 {
10 long result; /* fibonacci value */
11 long number; /* number input by user */
12
13 /* obtain integer from user */
14 printf( "Enter an integer: " );
15 scanf( "%ld", &number );
16
17 /* calculate fibonacci value for number input by user */
18 result = fibonacci( number );
19
20 /* display result */
21 printf( "Fibonacci( %ld ) = %ld\n", number, result );
22
23 return 0; /* indicates successful termination */
24
25 } /* end main */
26
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
27 /* Recursive definition of function fibonacci */
28 long fibonacci( long n ) Outline
29 {
30 /* base case */
31 if ( n == 0 || n == 1 ) {
32 return n;
33 } /* end if */
34 else { /* recursive step */
35 return fibonacci( n - 1 ) + fibonacci( n - 2 );
36 } /* end else */
37
38 } /* end function fibonacci */
Enter an integer: 0
Fibonacci( 0 ) = 0
Enter an integer: 1
Fibonacci( 1 ) = 1
Enter an integer: 2
Fibonacci( 2 ) = 1
Enter an integer: 3
Fibonacci( 3 ) = 2
Enter an integer: 4
Fibonacci( 4 ) = 3
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
Enter an integer: 5
Fibonacci( 5 ) = 5 Outline
Enter an integer: 6
Fibonacci( 6 ) = 8 Program Output
Enter an integer: 10
(continued)
Fibonacci( 10 ) = 55
Enter an integer: 20
Fibonacci( 20 ) = 6765
Enter an integer: 30
Fibonacci( 30 ) = 832040
Enter an integer: 35
Fibonacci( 35 ) = 9227465
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
fibonacci( 3 )
return 1 return 0
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.
www.almohandiss.com
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
www.almohandiss.com of 9.