EEE0115
Chapter 5: C Functions
C How to Program
1 Deitel & Deitel
2
Outline
Program Modules in C
Math Library Functions
Functions
Function Definitions
Function Prototypes
Calling Functions: Call by Value and Call by
Reference
Random Number Generation
Recursion
3
Program Modules in C
C programs can call user-defined
functions and built in library functions.
A function is called by function name
and argument
Function performs operations and
returns results
Functions can be considered as
modules in C
4
Math Library Functions
Used to perform math computations
To be able to use math library
functions, C proprams should include
<math.h> (#include <math.h>)
Example:
printf("%.2f", pow( 5, 2 ) );
All math functions return double data
Arguments may be constants, variables,
or expressions.
5
Functions
Functions inherently modularize
programs
The variables defined in function
definition are called local variables and
they are only be accessed in function.
Function parameters are also local
variables. They are used to
communicate between functions and
they are also local variables.
6
Functions
Advantages of Functions
Manageable program development
Software reusability
Avoid code repetition
7
Function Definitions
Function definition format
return-value-type function-name(parameter-
list)
{
declarations and statements
}
void as a return type indicates that
function returns nothing
Parameters given as a comma seperated
list.
Functions can not be defined inside other
functions.
If the function returns nothing, only return;
or nothing is provided.
8
Function Definitions
1 /* Fig. 5.3: fig05_03.c
2 Creating and using a programmer-defined function */
3 #include <stdio.h>
4
5 int square( int y ); /* function prototype */
6
7 /* function main begins program execution */
8 int main( void )
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
23 /* square function definition returns square of parameter */
24 int square( int y ) /* y is a copy of argument to function */
25 {
26 return y * y; /* returns square of y as an int */
27
28 } /* end function square */
1 4 9 16 25 36 49 64 81 100
9
Function Prototypes
Function prototype includes:
Function name
Parameters
Return type
Prototypes are needed if the function
definition is provided after main
program.
Example:
int maximum(int x, int y, int z),
The maximum function takes 3 integers
and returns integer value as a result.
10
Calling Functions: Call by Value and Call by
Reference
Call by value
A copy of the argument is created and
passed to function.
Modifications performed in function do
not effect the original value.
Call by reference
Original argument passed to function
Modifications in function effect the
original value.
11
Random Number Generation
rand function is defined in <stdlib.h>
rand returns a random number between
0 and RAND_MAX
To produce a random number between
1 and n
1 + (rand() % n) expression can be
used.
rand() % n returns a number
between 0 and n-1
12
Random Number Generation
srand function is defined in <stdlib.h>
It takes an integer seed and jumps to
that location in its random sequence
srand(seed)
srand(time(NULL));
time(NULL) returns the number of
seconds since January 1, 1970 and
therefore randomizes the seed.
13
Random Number Generation
1 /* Fig. 5.9: fig05_09.c
2 Randomizing die-rolling program */
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 /* function main begins program execution */
7 int main( void )
8 {
9 int i; /* counter */
10 unsigned seed; /* number used to seed random number generator */
11
12 printf( "Enter seed: " );
13 scanf( "%u", &seed ); /* note %u for unsigned */
14
15 srand( seed ); /* seed random number generator */
16
17 /* loop 10 times */
18 for ( i = 1; i <= 10; i++ ) {
19
14
Random Number Generation
20 /* pick a random number from 1 to 6 and output it */
21 printf( "%10d", 1 + ( rand() % 6 ) );
22
23 /* if counter is divisible by 5, begin a new line of output */
24 if ( i % 5 == 0 ) {
25 printf( "\n" );
26 } /* end if */
27
28 } /* end for */
29
30 return 0; /* indicates successful termination */
31
32 } /* end main */
Enter seed: 67
6 1 4 6 2
1 6 1 6 4
Enter seed: 867
2 4 6 1 6
1 1 3 6 2
Enter seed: 67
6 1 4 6 2
1 6 1 6 4
15
Recursion
Recursive functions call themselves.
A base case need to be provided.
Example:
5! = 5 * 4 * 3 * 2 *1
5! = 5 * 4!
4! = 4 * 3! ...
Base case (1! = 0! = 1)
16
Recursion
1 /* Fig. 5.14: fig05_14.c
2 Recursive factorial function */
3 #include <stdio.h>
4
5 long factorial( long number ); /* function prototype */
6
7 /* function main begins program execution */
8 int main( void )
9 {
10 int i; /* counter */
11
12 /* loop 11 times; during each iteration, calculate
13 factorial( i ) and display result */
14 for ( i = 0; 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
17
Recursion
22 /* recursive definition of function factorial */
23 long factorial( long number )
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 */
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800