Functions and An To Recursion: 2006 Pearson Education, Inc. All Rights Reserved
Functions and An To Recursion: 2006 Pearson Education, Inc. All Rights Reserved
Functions and An To Recursion: 2006 Pearson Education, Inc. All Rights Reserved
6
Functions and an
Introduction
to Recursion
2006 Pearson Education, Inc. All rights reserved.
2
6.1 Introduction
6.2 Program Components in C++
6.3 Math Library Functions
6.4 Function Definitions with Multiple Parameters
6.5 Function Prototypes and Argument Coercion
6.6 C++ Standard Library Header Files
6.7 Case Study: Random Number Generation
6.8 Case Study: Game of Chance and Introducing enum
6.9 Storage Classes
6.10 Scope Rules
6.11 Function Call Stack and Activation Records
6.12 Functions with Empty Parameter Lists
6.1 Introduction
• Divide and conquer technique
– Construct a large program from small, simple pieces (e.g.,
components)
• Functions
– Facilitate the design, implementation, operation and
maintenance of large programs
• C++ Standard Library math functions
• Functions
– Called methods or procedures in other languages
– Allow programmers to modularize a program by
separating its tasks into self-contained units
• Statements in function bodies are written only once
– Reused from perhaps several locations in a program
– Avoid repeating code
• Enable the divide-and-conquer approach
• Reusable in other programs
• Functions (Cont.)
– A function is invoked by a function call
• Called function either returns a result or simply returns to
the caller
• Function calls form hierarchical relationships
Data types
long double
double
float
unsigned long int (synonymous with unsigned long)
long int (synonymous with long)
unsigned int (synonymous with unsigned)
int
unsigned short int (synonymous with unsigned short)
short int (synonymous with short)
unsigned char
char
bool
C++ Standard
Explanation
Library header file
<typeinfo> Contains classes for runtime type identification (determining
data types at execution time). This header file is discussed in
Section 13.8.
<exception>, These header files contain classes that are used for exception
<stdexcept> handling (discussed in Chapter 16).
<memory> Contains classes and functions used by the C++ Standard
Library to allocate memory to the C++ Standard Library
containers. This header is used in Chapter 16, Exception
Handling.
<fstream> Contains function prototypes for functions that perform input
from files on disk and output to files on disk (discussed in
Chapter 17, File Processing). This header file replaces header file
<fstream.h>.
<string> Contains the definition of class string from the C++ Standard
Library (discussed in Chapter 18).
<sstream> Contains function prototypes for functions that perform input
from strings in memory and output to strings in memory
(discussed in Chapter 18, Class string and String Stream
Processing).
<functional> Contains classes and functions used by C++ Standard Library
algorithms. This header file is used in Chapter 23.
Face Frequency
1 999702
2 1000823
3 999378
4 998898
5 1000777
6 1000422 Each face value appears approximately 1,000,000 times
Enter seed: 67
6 1 4 6 2
1 6 1 6 4
Player rolled 6 + 6 = 12
Player loses
Player rolled 3 + 3 = 6
Point is 6
Player rolled 5 + 3 = 8
Player rolled 4 + 5 = 9
Player rolled 2 + 1 = 3
Player rolled 1 + 5 = 6
Player wins
Player rolled 1 + 3 = 4
Point is 4
Player rolled 4 + 6 = 10
Player rolled 2 + 4 = 6
Player rolled 6 + 4 = 10
Player rolled 2 + 3 = 5
Player rolled 2 + 4 = 6
Player rolled 1 + 1 = 2
Player rolled 4 + 4 = 8
Player rolled 4 + 3 = 7
Player loses
local x in main is 5
10 squared: 100
Fig. 6.14 | Function call stack after the operating system invokes main to execute the
application.
Fig. 6.15 | Function call stack after main invokes function square to perform the
calculation.
Fig. 6.16 | Function call stack after function square returns to main.