1.10 Functions
1.10 Functions
Outline
5.1 Introduction • Divide and conquer
5.2 Program Modules in C
5.3 Math Library Functions
5.4 Functions – Construct a program from smaller pieces or components
5.5 Function Definitions
5.6 Function Prototypes • These smaller pieces are called modules
5.7 Header Files
5.8 Calling Functions: Call by Value and Call by Reference
5.9 Random Number Generation – Each piece more manageable than the original program
5.10 Example: A Game of Chance
5.11 Storage Classes
5.12 Scope Rules
5.13 Recursion
5.14 Example Using Recursion: The Fibonacci Series
5.15 Recursion vs. Iteration
1
4/28/2022
2
4/28/2022
23 if ( y > max )
24 max = y;
b) A function (f_prod) to compute the product of the two 25
26 if ( z > max )
27 max = z;
matrices 28
29 return max;
30 }
Enter three integers: 22 85 17
2000 Prentice Hall, Inc. Maximum is: 85 Program Output
2000 Prentice Hall, Inc.
All rights reserved. All rights reserved.
3
4/28/2022
• srand function 5
6 int main() 2. Input value for seed
– <stdlib.h> 7 {
2.1 Use srand to
– Takes an integer seed and jumps to that location in its 8 int i;
9 unsigned seed; change random
"random" sequence 10
sequence
srand( seed ); 11 printf( "Enter seed: " );
2.2 Define Loop
– srand( time( NULL ) ); //load <time.h> 12 scanf( "%u", &seed );
13 srand( seed );
• time( NULL ) 3. Generate and
14
– Returns the time at which the program was compiled in 15 for ( i = 1; i <= 10; i++ ) {
output random
seconds numbers
16 printf( "%10d", 1 + ( rand() % 6 ) );
4
4/28/2022
Enter seed: 67
6 1 4 6 2
Outline
1 6 1 6 4 5.10 Example: A Game of Chance
Program Output
Enter seed: 867
2
1
4
1
6
3
1
6
6
2
• Craps simulator
• Rules
Enter seed: 67
6 1 4 6 2 – Roll two dice
1 6 1 6 4
• 7 or 11 on first throw, player wins
• 2, 3, or 12 on first throw, player loses
• 4, 5, 6, 8, 9, 10 - value becomes player's "point"
– Player must roll his point before rolling 7 to win
5
4/28/2022
Player rolled 6 + 6 = 12
Player loses
Outline
Player rolled 4 + 6 = 10 5.11 Storage Classes
Point is 10 Program Output
Player rolled 2 + 4 = 6
Player rolled
Player rolled
6
3
+
+
5
3
=
=
11
6 • Storage class specifiers
Player rolled 6 + 4 = 10
Player wins – Storage duration – how long an object exists in memory
Player rolled 1 + 3 = 4 – Scope – where object can be referenced in program
Point is 4
Player rolled 1 + 4 = 5 – Linkage – specifies the files in which an identifier is known
Player rolled 5 + 4 = 9
Player rolled 4 + 6 = 10 (more in Chapter 14)
Player rolled 6 + 3 = 9
Player rolled
Player rolled
1
5
+
+
2
2
=
=
3
7
• Automatic storage
Player loses
– Object created and destroyed within its block
– auto: default for local variables
auto double x, y;
– register: tries to put variable into high-speed registers
• Can only be used for automatic variables
register int counter = 1;
6
4/28/2022
• Block scope 6
7
void b( void );
void c( void );
/* function prototype */
/* function prototype */
1.1 Initialize global
8
– Identifier declared inside a block 9 int x = 1; /* global variable */
variable
31
32 printf( "local x in main is %d\n", x ); Outline local x in outer scope of main is 5
Outline
33 return 0;
local x in inner scope of main is 7
34 } local x in outer scope of main is 5
35
3.1 Function Program Output
36 void a( void ) definitions local x in a is 25 after entering a
local x in a is 26 before exiting a
37 {
38 int x = 25; /* initialized each time a is called */ local static x is 50 on entering b
39 local static x is 51 on exiting b
40 printf( "\nlocal x in a is %d after entering a\n", x );
global x is 1 on entering c
41 ++x;
global x is 10 on exiting c
42 printf( "local x in a is %d before exiting a\n", x );
43 } local x in a is 25 after entering a
44 local x in a is 26 before exiting a
45 void b( void )
local static x is 51 on entering b
46 { local static x is 52 on exiting b
47 static int x = 50; /* static initialization only */
48 /* first time b is called */ global x is 10 on entering c
global x is 100 on exiting c
49 printf( "\nlocal static x is %d on entering b\n", x );
local x in main is 5
50 ++x;
51 printf( "local static x is %d on exiting b\n", x );
52 }
53
54 void c( void )
55 {
56 printf( "\nglobal x is %d on entering c\n", x );
57 x *= 10;
58 printf( "global x is %d on exiting c\n", x );
59 } 2000 Prentice Hall, Inc. 2000 Prentice Hall, Inc.
All rights reserved. All rights reserved.
7
4/28/2022
5.14 Example Using Recursion: The 5.14 Example Using Recursion: The
Fibonacci Series Fibonacci Series
• Fibonacci series: 0, 1, 1, 2, 3, 5, 8... • Set of recursive calls to function fibonacci
– Each number is the sum of the previous two
f( 3 )
– Can be solved recursively:
• fib( n ) = fib( n - 1 ) + fib( n – 2 )
– Code for the fibaonacci function
return f( 2 ) + f( 1 )
long fibonacci( long n )
{
if (n == 0 || n == 1) // base case return f( 1 ) + f( 0 ) return 1
return n;
else
return fibonacci( n - 1) + return 1 return 0
fibonacci( n – 2 );
}
8
4/28/2022
Enter an integer: 1
Fibonacci(1) = 1
2000 Prentice Hall, Inc. 2000 Prentice Hall, Inc.
All rights reserved. All rights reserved.