0% found this document useful (0 votes)
19 views25 pages

Chapitre8 Fonctions

This document discusses functions in C programming. It introduces functions as a way to divide programs into modular pieces. It covers defining functions, passing arguments to functions, and using built-in math functions from the standard library like sqrt, pow, and trigonometric functions. Examples are given to demonstrate defining and calling user-defined functions.

Uploaded by

diez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views25 pages

Chapitre8 Fonctions

This document discusses functions in C programming. It introduces functions as a way to divide programs into modular pieces. It covers defining functions, passing arguments to functions, and using built-in math functions from the standard library like sqrt, pow, and trigonometric functions. Examples are given to demonstrate defining and calling user-defined functions.

Uploaded by

diez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

www.almohandiss.

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

• In this chapter, you will learn:


– To understand how to construct programs modularly
from small pieces called functions..
– To introduce the common math functions available in
the C standard library.
– To be able to create new functions.
– To understand the mechanisms used to pass information
between functions.
– To introduce simulation techniques using random
number generation.
– To understand how to write and use functions that call
themselves.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

5.3 Math Library Functions


• Math library functions
– perform common mathematical calculations
– #include <math.h>
• Format for calling functions
– FunctionName( argument );
• If multiple arguments, use comma-separated list
– printf( "%.2f", sqrt( 900.0 ) );
• Calls function sqrt, which returns the square root of its
argument
• All math functions return data type double
– Arguments may be constants, variables, or expressions

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

www.almohandiss.com of 9.
www.almohandiss.com

Math Library Functions

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

exp( x ) e x p o n e n tia l fu n c tio n e x e x p ( 1 . 0 ) i s 2.718282


e x p ( 2 . 0 ) i s 7.389056
log( x ) n a tu ra l lo g a rith m o f x l o g ( 2 . 71828 2 ) is 1.0
l o g ( 7 . 38905 6 ) is 2.0
(b a se e )
log10( x ) lo g a rith m o f x (b a s e 1 0 ) l o g 1 0( 1.0 ) i s 0. 0
l o g 1 0( 10. 0 ) is 1 .0
l o g 1 0( 100 .0 ) i s 2.0
fabs( x ) a b s o lu te v a lu e o f x f a b s ( 5.0 ) is 5 .0
f a b s ( 0.0 ) is 0 .0
f a b s ( -5.0 ) i s 5. 0
ceil( x ) ro u n d s x to th e s m a lle s t c e i l ( 9.2 ) is 1 0. 0
c e i l ( -9.8 ) i s -9 .0
in te g e r n o t le s s th a n x
floor( x ) ro u n d s x to th e la rg e s t floor( 9.2 ) is 9.0
floor( -9.8 ) is -10.0
in te g e r n o t g re a te r th a n x
pow( x, y ) x ra is e d to p o w e r y (x y) p o w ( 2, 7 ) is 128.0
p o w ( 9, .5 ) is 3.0
fmod( x, y ) re m a in d e r o f x /y a s a f m o d( 13.657, 2.333 ) is
1 . 9 92
flo a tin g p o in t n u m b e r
sin( x ) trig o n o m e tric s in e o f x sin( 0.0 ) is 0.0
(x in ra d ia n s )
cos( x ) trig o n o m e tric c o s in e o f x cos( 0.0 ) is 1.0
(x in ra d ia n s )
tan( x ) trig o n o m e tric ta n g e n t o f x tan( 0.0 ) is 0.0
(x in ra d ia n s )
F ig . 5 . 2 C o m m o n ly u s e d m a t h lib r a r y f u n c t io n s .

© 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

return-value-type function-name( parameter-list )


{
declarations and statements
}

© 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)

1 4 9 16 25 36 49 64 81 100 Program Output

© 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

Data types printf conversion scanf conversion


specifications specifications
long double %Lf %Lf
double %f %lf
float %f %f
unsigned long int %lu %lu
long int %ld %ld
unsigned int %u %u
int %d %d
short %hd %hd
char %c %c
Fig. 5.5 Prom otion hierarchy for data types.

© 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>

• Custom header files


– Create file with functions
– Save as filename.h
– Load in other files with #include "filename.h"
– Reuse functions

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

www.almohandiss.com of 9.
www.almohandiss.com

Calling Functions: Call by Value and Call by


Reference
• Call by value
– Copy of argument passed to function
– Changes in function do not effect original
– Use when function does not need to modify argument
• Avoids accidental changes
• Call by reference
– Passes original argument
– Changes in function effect original
– Only used with trusted functions
• For now, we focus on call by value

© 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

( a ) Se q u en c e o f re c ursive c a lls. ( b ) Va lue s re turne d fro m e a c h re cu rsive c a ll.

© 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

Example Using Recursion: The Fibonacci


Series
• Fibonacci series: 0, 1, 1, 2, 3, 5, 8...
– Each number is the sum of the previous two
– Can be solved recursively:
• fib( n ) = fib( n - 1 ) + fib( n – 2 )
– Code for the fibonacci function
long fibonacci( long n )
{
if (n == 0 || n == 1) // base case
return n;
else
return fibonacci( n - 1) +
fibonacci( n – 2 );
}

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

www.almohandiss.com of 9.
www.almohandiss.com

Example Using Recursion: The Fibonacci


Series
• Set of recursive calls to function fibonacci
f( 3 )

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

Example Using Recursion: The Fibonacci


Series

fibonacci( 3 )

return fibonacci( 2 ) + fibonacci( 1 )

return fibonacci( 1 ) + fibonacci( 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

Recursion vs. Iteration


• Repetition
– Iteration: explicit loop
– Recursion: repeated function calls
• Termination
– Iteration: loop condition fails
– Recursion: base case recognized
• Both can have infinite loops
• Balance
– Choice between performance (iteration) and good software
engineering (recursion)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

www.almohandiss.com of 9.

You might also like