SlideShare a Scribd company logo
Chapter 6 of C++ How to Program, 9/e
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 Develop and maintain a large program by constructing it
from small, simple pieces, or components.
◦ divide and conquer.
 Emphasize how to declare and use functions to facilitate
the design, implementation, operation and maintenance
of large programs.
 Function prototypes and how the compiler uses them to
convert the type of an argument in a function call to the
type specified in a function’s parameter list, if necessary.
 Simulation techniques with random number generation.
 C++’s scope rules.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 How C++ keeps track of which function is currently
executing, how parameters and other local variables
of functions are maintained in memory and how a
function knows where to return after it completes
execution.
 Function overloading.
 Function templates.
 Recursion.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 C++ programs are typically written by combining
“prepackaged” functions and classes available in the C++
Standard Library with new functions and classes you
write.
 The C++ Standard Library provides a rich collection of
functions.
 Functions allow you to modularize a program by
separating its tasks into self-contained units.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 A function is invoked by a function call, and when the
called function completes its task, it either returns a
result or simply returns control to the caller.
 An analogy is the hierarchical form of management
(Figure 6.1).
◦ A boss (similar to the calling function) asks a worker (similar to
the called function) to perform a task and report back (i.e.,
return) the results after completing the task.
◦ The boss does not know how the worker performs its tasks.
◦ The worker may also call other workers, unbeknownst to the
boss.
 This hiding of implementation details promotes good
software engineering.
 The relationship does not need to be hierarchical, but
often it is, which makes it easier to test, debug, update
and maintain programs.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 Sometimes functions are not members of a class.
◦ Called global functions.
 The <cmath> header provides a collection of functions that
enable you to perform common mathematical calculations.
 All functions in the <cmath> header are global functions—
therefore, each is called simply by specifying the name of the
function followed by parentheses containing the function’s
arguments.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 Some math library functions are summarized in
Fig. 6.2.
◦ In the figure, the variables x and y are of type
double.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 In this section, we create a user-defined function called
maximum (Fig. 6.3) that returns the largest of its three int
arguments.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 For a function not defined in a class, you must either
define the function before using it or you must declare
that the function exists, as we do in line 7 of Fig. 6.3:
◦ int maximum(int x, int y, int z); // function prototype
 This is a function prototype, which describes the
maximum function without revealing its implementation.
 A function prototype is a declaration of a function
◦ tells the compiler the function’s name, its return type and the
types of its parameters.
 This function prototype indicates that the function
returns an int, has the name maximum and requires
three int parameters to perform its task.
 The function prototype is the same as the first line of the
corresponding function definition (line 20), but ends
with a required semicolon.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 When compiling the program, the compiler uses
the prototype to
◦ Ensure that maximum’s first line matches its prototype.
◦ Check that the call to maximum contains the correct
number and types of arguments, and that the types of
the arguments are in the correct order.
◦ Ensure that the value returned by the function can be
used correctly in the expression that called the function.
◦ Ensure that each argument is consistent with the type of
the corresponding parameter.
 If the arguments passed to a function do not match the types
specified in the function’s prototype, the compiler attempts to
convert the arguments to those types.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 Multiple parameters are specified in both the
function prototype and the function header as a
comma-separated list, as are multiple arguments
in a function call.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 In a function that does not return a result (i.e., it has
a void return type), we showed that control returns
when the program reaches the function-ending right
brace.
 You also can explicitly return control to the caller by
executing the statement
◦ return;
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 A function prototype is required unless the function
is defined before it’s used.
 When you use a standard library function like sqrt,
you do not have access to the function’s definition,
therefore it cannot be defined in your code before
you call the function.
 Instead, you must include its corresponding header
(<cmath>), which contains the function’s prototype.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 The portion of a function prototype that includes the
name of the function and the types of its arguments is
called the function signature or simply the
signature.
◦ Signature does not specify the function’s return type.
 The scope of a function is the region of a program in
which the function is known and accessible.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 An important feature of function prototypes is argument
coercion
◦ forcing arguments to the appropriate types specified by the
parameter declarations.
◦ These conversions occur as specified by C++’s promotion rules.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 The promotion rules indicate how to convert between
types without losing data.
 The promotion rules apply to expressions containing
values of two or more data types
◦ also referred to as mixed-type expressions.
 The type of each value in a mixed-type expression is
promoted to the “highest” type in the expression.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 Figure 6.4 lists the arithmetic data types in order
from “highest type” to “lowest type.”
 Converting values to lower fundamental types can
result in incorrect values.
 Therefore, a value can be converted to a lower
fundamental type only by explicitly assigning the
value to a variable of lower type or by using a cast
operator.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 The C++ Standard Library is divided into many portions,
each with its own header file.
 The header files contain the function prototypes for the
related functions that form each portion of the library.
 The header files also contain definitions of various class
types and functions, as well as constants needed by
those functions.
 A header file “instructs” the compiler on how to
interface with library and user-written components.
 Figure 6.5 lists some common C++ Standard Library
header files, most of which are discussed later in the
book.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 The element of chance can be introduced into computer
applications by using the C++ Standard Library function
rand.
 The function rand generates an unsigned integer
between 0 and RAND_MAX (a symbolic constant defined
in the <cstdlib> header file).
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 The value of RAND_MAX must be at least 32767—the
maximum positive value for a two-byte (16-bit) integer.
 For GNU C++, the value of RAND_MAX is 2147483647; for
Visual Studio, the value of RAND_MAX is 32767.
 If rand truly produces integers at random, every
number between 0 and RAND_MAX has an equal chance
(or probability) of being chosen each time rand is called.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 The function prototype for the rand function is in
<cstdlib>.
 To produce integers in the range 0 to 5, we use the
modulus operator (%) with rand:
 rand() % 6
◦ This is called scaling.
◦ The number 6 is called the scaling factor. Six values are
produced.
 We can shift the range of numbers produced by
adding a value.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 To show that the numbers produced by rand occur
with approximately equal likelihood, Fig. 6.7
simulates 60,000,000 rolls of a die.
 Each integer in the range 1 to 6 should appear
approximately 10,000,000 times.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 Prior to C++14, you’d represent the integer value
60,000,000 as 60000000 in a program.
 To make numeric literals more readable, C++14
allows you to insert between groups of digits in
numeric literals the digit separator ' (a single-quote
character)
◦ 60'000'000 represents the integer value 60,000,000.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 Executing the program of Fig. 6.6 again produces
exactly the same sequence of values.
◦ Repeatability is an important characteristic of rand.
◦ Can help with testing and debugging.
 rand generates pseudorandom numbers.
◦ a sequence of numbers that appears to be random.
◦ sequence repeats itself each time the program executes.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 Once a program has been debugged, it can be
conditioned to produce a different sequence of
random numbers for each execution.
 This is called randomizing and is accomplished
with the C++ Standard Library function srand.
◦ Takes an unsigned integer argument and seeds rand to
produce a different sequence of random numbers for
each execution.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
Seeding the Random Number Generator with srand
 Figure 6.8 demonstrates function srand.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 To randomize without having to enter a seed
 srand(static_cast<unsigned int>(time(0)));
 Causes the computer to read its clock to obtain the
value for the seed.
 Function time (with the argument 0 as written in
the preceding statement) returns the current time
as the number of seconds since January 1, 1970, at
midnight Greenwich Mean Time (GMT).
 The function prototype for time is in <ctime>.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 To produce random numbers in a specific range use:
◦ type variableName = shiftingValue + rand() % scalingFactor;
 shiftingValue is equal to the first number in the
desired range of consecutive integers
 scalingFactor is equal to the width of the de-sired
range of consecutive integers.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 According to CERT, function rand does not have
“good statistical properties” and can be
predictable, which makes programs that use rand
less secure (CERT guideline MSC30-CPP).
 C++11 provides a new, more secure library of
random-number capabilities that can produce
nondeterministic random numbers that can’t be
predicted.
 Such random-number generators are used in
simulations and security scenarios where
predictability is undesirable.
◦ New capabilities are located in the C++ Standard
Library’s <random> header.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 For flexibility based on how random numbers are
used in programs, C++11 provides many classes
that represent various random-number
generation engines and distributions.
◦ An engine implements a random-number generation
algorithm that produce pseudorandom numbers.
◦ A distribution controls the range of values produced by
an engine, the types of those values (e.g., int, double, etc.)
and the statistical properties of the values.
 uniform_int_distribution evenly distributes
pseudorandom integers over a specified range of
values.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
Rolling a Six-Sided Die
 Figure 6.10 uses the default_random_engine and the
uniform_int_distribution to roll a six-sided die.
◦ default_random_engine object named engine.
◦ Its constructor argument seeds the random-number
generation engine with the current time.
◦ If you don’t pass a value to the constructor, the default
seed will be used and the program will produce the same
sequence of numbers each time it executes.
◦ randomInt—a uniform_int_distribution object that
produces unsigned int values (as specified by <unsigned
int>) in the range 1 to 6 (as specified by the constructor
arguments).
◦ The expression randomInt(engine) returns one unsigned
int value in the range 1 to 6.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 The notation <unsigned int> indicates that
uniform_int_distribution is a class template.
 In this case, any integer type can be specified in the angle
brackets (< and >).
 In Chapter 18, we discuss how to create class templates
and various other chapters show how to use existing class
templates from the C++ Standard Library.
 For now, you should feel comfortable using class template
uniform_int_distribution by mimicking the syntax shown
in the example.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 The portion of the program where an identifier can
be used is known as its scope.
 This section discusses
◦ block scope
◦ global namespace scope
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 Identifiers declared inside a block have block scope, which
begins at the identifier’s declaration and ends at the
terminating right brace (}) of the enclosing block.
◦ Local variables have block scope, as do function parameters.
 Any block can contain variable declarations.
 In nested blocks, if an identifier in an outer block has the
same name as an identifier in an inner block, the one in the
outer block is “hidden” until the inner block terminates.
 The inner block “sees” its own local variable’s value and
not that of the enclosing block’s identically named variable.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 An identifier declared outside any function or class
has global namespace scope.
◦ “known” in all functions from the point at which it’s
declared until the end of the file.
 Function definitions, function prototypes placed
outside a function, class definitions and global
variables all have global namespace scope.
 Global variables are created by placing variable
declarations outside any class or function definition.
Such variables retain their values throughout a
program’s execution.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
Scope Demonstration
 The program of Fig. 6.11 demonstrates scoping
issues with global variables, automatic local
variables and static local variables.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 To understand how C++ performs function calls, we
first need to consider a data structure (i.e., collection
of related data items) known as a stack.
 Analogous to a pile of dishes.
◦ When a dish is placed on the pile, it’s normally placed at the
top—referred to as pushing.
◦ Similarly, when a dish is removed from the pile, it’s
normally removed from the top—referred to as popping.
 Last-in, first-out (LIFO) data structures—the last
item pushed (inserted) is the first item popped
(removed).
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
Function-Call Stack
 One of the most important mechanisms for computer science
students to understand is the function call stack (or program
execution stack).
◦ supports the function call/return mechanism.
◦ Also supports the creation, maintenance and destruction of each
called function’s automatic variables.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
Stack Frames
 Each function eventually must return control to the
function that called it.
 Each time a function calls another function, an entry is
pushed onto the function call stack.
◦ This entry, called a stack frame or an activation record,
contains the return address that the called function needs
in order to return to the calling function.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
Automatic Local Variables and Stack Frames
 When a function call returns, the stack frame for the
function call is popped, and control transfers to the return
address in the popped stack frame.
Stack Overflow
 The amount of memory in a computer is finite, so only a
certain amount of memory can be used to store activation
records on the function call stack.
 If more function calls occur than can have their activation
records stored on the function call stack, an a fatal error
known as stack overflow occurs.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
Function Call Stack in Action
 Now let’s consider how the call stack supports the
operation of a square function called by main (Fig. 6.12).
 First the operating system calls main—this pushes an
activation record onto the stack (shown in Fig. 6.13).
 The activation record tells main how to return to the
operating system (i.e., transfer to return address R1) and
contains the space for main’s automatic variable (i.e., a,
which is initialized to 10).
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 Function main—before returning to the operating
system—now calls function square in line 12 of Fig. 6.12.
 This causes a stack frame for square (lines 16–18) to be
pushed onto the function call stack (Fig. 6.14).
 This stack frame contains the return address that square
needs to return to main (i.e., R2) and the memory for
square’s automatic variable (i.e., x).
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 After square calculates the square of its argument, it
needs to return to main—and no longer needs the memory
for its automatic variable x. So square’s stack frame is
popped from the stack—giving square the return location
in main (i.e., R2) and losing square’s automatic variable.
 Figure 6.15 shows the function call stack after square’s
activation record has been popped.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 Two ways to pass arguments to functions in many
programming languages are pass-by-value and pass-by-
reference.
 When an argument is passed by value, a copy of the
argument’s value is made and passed (on the function
call stack) to the called function.
◦ Changes to the copy do not affect the original variable’s
value in the caller.
 To specify a reference to a constant, place the const
qualifier before the type specifier in the parameter
declaration.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 With pass-by-reference, the caller gives the called function
the ability to access the caller’s data directly, and to modify
that data.
 A reference parameter is an alias for its corresponding
argument in a function call.
 To indicate that a function parameter is passed by
reference, simply follow the parameter’s type in the
function prototype by an ampersand (&); use the same
convention when listing the parameter’s type in the
function header.
 Figure 6.17 compares pass-by-value and pass-by-reference
with reference parameters.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 References can also be used as aliases for other
variables within a function.
 Reference variables must be initialized in their
declarations and cannot be reassigned as aliases to
other variables.
 Once a reference is declared as an alias for another
variable, all operations supposedly performed on
the alias are actually performed on the original
variable.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
 To specify that a reference parameter should not be
allowed to modify the corresponding argument,
place the const qualifier before the type name in
the parameter’s declaration.
 string objects can be large, so they (and objects in
general) should be passed to functions by reference.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
 Functions can return references, but this can be
dangerous.
 When returning a reference to a variable declared in
the called function, the variable should be declared
static in that function.
©1992-2017 by Pearson Education, Inc.
All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.

More Related Content

Similar to This is a chapter about arrays and vectors in c++ (20)

PPT
cpphtp4_PPT_03.ppt
Suleman Khan
 
PPTX
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
PPTX
Chapter 1 (2) array and structure r.pptx
abenezertekalign118
 
PPT
Cpphtp4 ppt 03
sanya6900
 
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
PPT
C++ Functions.ppt
kanaka vardhini
 
PPT
power point presentation on object oriented programming functions concepts
bhargavi804095
 
PPT
Lecture 4
Mohammed Saleh
 
PPTX
Function C++
Shahzad Afridi
 
PPT
C++ Functions.ppt
WaheedAnwar20
 
PPT
C++ functions
Dawood Jutt
 
PPT
C++ functions
Dawood Jutt
 
PDF
Pro
TeshaleSiyum
 
PPT
Fp201 unit5 1
rohassanie
 
DOCX
Functions assignment
Ahmad Kamal
 
PDF
(3) cpp procedural programming
Nico Ludwig
 
PPT
C++ functions presentation by DHEERAJ KATARIA
Dheeraj Kataria
 
PPT
Lecture#6 functions in c++
NUST Stuff
 
PPTX
Fundamental of programming Fundamental of programming
LidetAdmassu
 
cpphtp4_PPT_03.ppt
Suleman Khan
 
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
Chapter 1 (2) array and structure r.pptx
abenezertekalign118
 
Cpphtp4 ppt 03
sanya6900
 
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
C++ Functions.ppt
kanaka vardhini
 
power point presentation on object oriented programming functions concepts
bhargavi804095
 
Lecture 4
Mohammed Saleh
 
Function C++
Shahzad Afridi
 
C++ Functions.ppt
WaheedAnwar20
 
C++ functions
Dawood Jutt
 
C++ functions
Dawood Jutt
 
Fp201 unit5 1
rohassanie
 
Functions assignment
Ahmad Kamal
 
(3) cpp procedural programming
Nico Ludwig
 
C++ functions presentation by DHEERAJ KATARIA
Dheeraj Kataria
 
Lecture#6 functions in c++
NUST Stuff
 
Fundamental of programming Fundamental of programming
LidetAdmassu
 

Recently uploaded (20)

PDF
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
PPTX
darshai cross section and river section analysis
muk7971
 
PDF
13th International Conference on Artificial Intelligence, Soft Computing (AIS...
ijait
 
PDF
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
PPTX
Electrical_Safety_EMI_EMC_Presentation.pptx
drmaneharshalid
 
PPTX
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
PDF
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
PPTX
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PDF
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
PDF
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
PDF
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
PPTX
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
PPSX
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
PPTX
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
PDF
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
PDF
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
PDF
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
PDF
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
NFPA 10 - Estandar para extintores de incendios portatiles (ed.22 ENG).pdf
Oscar Orozco
 
darshai cross section and river section analysis
muk7971
 
13th International Conference on Artificial Intelligence, Soft Computing (AIS...
ijait
 
Python Mini Project: Command-Line Quiz Game for School/College Students
MPREETHI7
 
Electrical_Safety_EMI_EMC_Presentation.pptx
drmaneharshalid
 
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
Comparison of Flexible and Rigid Pavements in Bangladesh
Arifur Rahman
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
Bayesian Learning - Naive Bayes Algorithm
Sharmila Chidaravalli
 
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
Introduction to File Transfer Protocol with commands in FTP
BeulahS2
 
OOPS Concepts in Python and Exception Handling
Dr. A. B. Shinde
 
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
A Brief Introduction About Robert Paul Hardee
Robert Paul Hardee
 
bs-en-12390-3 testing hardened concrete.pdf
ADVANCEDCONSTRUCTION
 
Ad

This is a chapter about arrays and vectors in c++

  • 1. Chapter 6 of C++ How to Program, 9/e ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 2. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 3. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 4. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 5.  Develop and maintain a large program by constructing it from small, simple pieces, or components. ◦ divide and conquer.  Emphasize how to declare and use functions to facilitate the design, implementation, operation and maintenance of large programs.  Function prototypes and how the compiler uses them to convert the type of an argument in a function call to the type specified in a function’s parameter list, if necessary.  Simulation techniques with random number generation.  C++’s scope rules. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 6.  How C++ keeps track of which function is currently executing, how parameters and other local variables of functions are maintained in memory and how a function knows where to return after it completes execution.  Function overloading.  Function templates.  Recursion. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 7.  C++ programs are typically written by combining “prepackaged” functions and classes available in the C++ Standard Library with new functions and classes you write.  The C++ Standard Library provides a rich collection of functions.  Functions allow you to modularize a program by separating its tasks into self-contained units. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 8. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 9.  A function is invoked by a function call, and when the called function completes its task, it either returns a result or simply returns control to the caller.  An analogy is the hierarchical form of management (Figure 6.1). ◦ A boss (similar to the calling function) asks a worker (similar to the called function) to perform a task and report back (i.e., return) the results after completing the task. ◦ The boss does not know how the worker performs its tasks. ◦ The worker may also call other workers, unbeknownst to the boss.  This hiding of implementation details promotes good software engineering.  The relationship does not need to be hierarchical, but often it is, which makes it easier to test, debug, update and maintain programs. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 10. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 11.  Sometimes functions are not members of a class. ◦ Called global functions.  The <cmath> header provides a collection of functions that enable you to perform common mathematical calculations.  All functions in the <cmath> header are global functions— therefore, each is called simply by specifying the name of the function followed by parentheses containing the function’s arguments. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 12. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 13.  Some math library functions are summarized in Fig. 6.2. ◦ In the figure, the variables x and y are of type double. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 14. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 15. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 16.  In this section, we create a user-defined function called maximum (Fig. 6.3) that returns the largest of its three int arguments. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 17. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 18. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 19. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 20.  For a function not defined in a class, you must either define the function before using it or you must declare that the function exists, as we do in line 7 of Fig. 6.3: ◦ int maximum(int x, int y, int z); // function prototype  This is a function prototype, which describes the maximum function without revealing its implementation.  A function prototype is a declaration of a function ◦ tells the compiler the function’s name, its return type and the types of its parameters.  This function prototype indicates that the function returns an int, has the name maximum and requires three int parameters to perform its task.  The function prototype is the same as the first line of the corresponding function definition (line 20), but ends with a required semicolon. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 21. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 22.  When compiling the program, the compiler uses the prototype to ◦ Ensure that maximum’s first line matches its prototype. ◦ Check that the call to maximum contains the correct number and types of arguments, and that the types of the arguments are in the correct order. ◦ Ensure that the value returned by the function can be used correctly in the expression that called the function. ◦ Ensure that each argument is consistent with the type of the corresponding parameter.  If the arguments passed to a function do not match the types specified in the function’s prototype, the compiler attempts to convert the arguments to those types. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 23. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 24. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 25. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 26.  Multiple parameters are specified in both the function prototype and the function header as a comma-separated list, as are multiple arguments in a function call. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 27. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 28. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 29. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 30.  In a function that does not return a result (i.e., it has a void return type), we showed that control returns when the program reaches the function-ending right brace.  You also can explicitly return control to the caller by executing the statement ◦ return; ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 31.  A function prototype is required unless the function is defined before it’s used.  When you use a standard library function like sqrt, you do not have access to the function’s definition, therefore it cannot be defined in your code before you call the function.  Instead, you must include its corresponding header (<cmath>), which contains the function’s prototype. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 32. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 33. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 34.  The portion of a function prototype that includes the name of the function and the types of its arguments is called the function signature or simply the signature. ◦ Signature does not specify the function’s return type.  The scope of a function is the region of a program in which the function is known and accessible. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 35. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 36.  An important feature of function prototypes is argument coercion ◦ forcing arguments to the appropriate types specified by the parameter declarations. ◦ These conversions occur as specified by C++’s promotion rules. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 37.  The promotion rules indicate how to convert between types without losing data.  The promotion rules apply to expressions containing values of two or more data types ◦ also referred to as mixed-type expressions.  The type of each value in a mixed-type expression is promoted to the “highest” type in the expression. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 38.  Figure 6.4 lists the arithmetic data types in order from “highest type” to “lowest type.”  Converting values to lower fundamental types can result in incorrect values.  Therefore, a value can be converted to a lower fundamental type only by explicitly assigning the value to a variable of lower type or by using a cast operator. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 39. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 40. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 41.  The C++ Standard Library is divided into many portions, each with its own header file.  The header files contain the function prototypes for the related functions that form each portion of the library.  The header files also contain definitions of various class types and functions, as well as constants needed by those functions.  A header file “instructs” the compiler on how to interface with library and user-written components.  Figure 6.5 lists some common C++ Standard Library header files, most of which are discussed later in the book. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 42. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 43. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 44. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 45. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 46.  The element of chance can be introduced into computer applications by using the C++ Standard Library function rand.  The function rand generates an unsigned integer between 0 and RAND_MAX (a symbolic constant defined in the <cstdlib> header file). ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 47.  The value of RAND_MAX must be at least 32767—the maximum positive value for a two-byte (16-bit) integer.  For GNU C++, the value of RAND_MAX is 2147483647; for Visual Studio, the value of RAND_MAX is 32767.  If rand truly produces integers at random, every number between 0 and RAND_MAX has an equal chance (or probability) of being chosen each time rand is called. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 48.  The function prototype for the rand function is in <cstdlib>.  To produce integers in the range 0 to 5, we use the modulus operator (%) with rand:  rand() % 6 ◦ This is called scaling. ◦ The number 6 is called the scaling factor. Six values are produced.  We can shift the range of numbers produced by adding a value. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 49. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 50.  To show that the numbers produced by rand occur with approximately equal likelihood, Fig. 6.7 simulates 60,000,000 rolls of a die.  Each integer in the range 1 to 6 should appear approximately 10,000,000 times. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 51. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 52. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 53. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 54. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 55.  Prior to C++14, you’d represent the integer value 60,000,000 as 60000000 in a program.  To make numeric literals more readable, C++14 allows you to insert between groups of digits in numeric literals the digit separator ' (a single-quote character) ◦ 60'000'000 represents the integer value 60,000,000. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 56.  Executing the program of Fig. 6.6 again produces exactly the same sequence of values. ◦ Repeatability is an important characteristic of rand. ◦ Can help with testing and debugging.  rand generates pseudorandom numbers. ◦ a sequence of numbers that appears to be random. ◦ sequence repeats itself each time the program executes. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 57. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 58.  Once a program has been debugged, it can be conditioned to produce a different sequence of random numbers for each execution.  This is called randomizing and is accomplished with the C++ Standard Library function srand. ◦ Takes an unsigned integer argument and seeds rand to produce a different sequence of random numbers for each execution. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 59. Seeding the Random Number Generator with srand  Figure 6.8 demonstrates function srand. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 60. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 61. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 62. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 63. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 64.  To randomize without having to enter a seed  srand(static_cast<unsigned int>(time(0)));  Causes the computer to read its clock to obtain the value for the seed.  Function time (with the argument 0 as written in the preceding statement) returns the current time as the number of seconds since January 1, 1970, at midnight Greenwich Mean Time (GMT).  The function prototype for time is in <ctime>. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 65.  To produce random numbers in a specific range use: ◦ type variableName = shiftingValue + rand() % scalingFactor;  shiftingValue is equal to the first number in the desired range of consecutive integers  scalingFactor is equal to the width of the de-sired range of consecutive integers. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 66.  According to CERT, function rand does not have “good statistical properties” and can be predictable, which makes programs that use rand less secure (CERT guideline MSC30-CPP).  C++11 provides a new, more secure library of random-number capabilities that can produce nondeterministic random numbers that can’t be predicted.  Such random-number generators are used in simulations and security scenarios where predictability is undesirable. ◦ New capabilities are located in the C++ Standard Library’s <random> header. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 67.  For flexibility based on how random numbers are used in programs, C++11 provides many classes that represent various random-number generation engines and distributions. ◦ An engine implements a random-number generation algorithm that produce pseudorandom numbers. ◦ A distribution controls the range of values produced by an engine, the types of those values (e.g., int, double, etc.) and the statistical properties of the values.  uniform_int_distribution evenly distributes pseudorandom integers over a specified range of values. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 68. Rolling a Six-Sided Die  Figure 6.10 uses the default_random_engine and the uniform_int_distribution to roll a six-sided die. ◦ default_random_engine object named engine. ◦ Its constructor argument seeds the random-number generation engine with the current time. ◦ If you don’t pass a value to the constructor, the default seed will be used and the program will produce the same sequence of numbers each time it executes. ◦ randomInt—a uniform_int_distribution object that produces unsigned int values (as specified by <unsigned int>) in the range 1 to 6 (as specified by the constructor arguments). ◦ The expression randomInt(engine) returns one unsigned int value in the range 1 to 6. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 69.  The notation <unsigned int> indicates that uniform_int_distribution is a class template.  In this case, any integer type can be specified in the angle brackets (< and >).  In Chapter 18, we discuss how to create class templates and various other chapters show how to use existing class templates from the C++ Standard Library.  For now, you should feel comfortable using class template uniform_int_distribution by mimicking the syntax shown in the example. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 70. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 71. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 72.  The portion of the program where an identifier can be used is known as its scope.  This section discusses ◦ block scope ◦ global namespace scope ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 73.  Identifiers declared inside a block have block scope, which begins at the identifier’s declaration and ends at the terminating right brace (}) of the enclosing block. ◦ Local variables have block scope, as do function parameters.  Any block can contain variable declarations.  In nested blocks, if an identifier in an outer block has the same name as an identifier in an inner block, the one in the outer block is “hidden” until the inner block terminates.  The inner block “sees” its own local variable’s value and not that of the enclosing block’s identically named variable. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 74. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 75. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 76.  An identifier declared outside any function or class has global namespace scope. ◦ “known” in all functions from the point at which it’s declared until the end of the file.  Function definitions, function prototypes placed outside a function, class definitions and global variables all have global namespace scope.  Global variables are created by placing variable declarations outside any class or function definition. Such variables retain their values throughout a program’s execution. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 77. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 78. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 79. Scope Demonstration  The program of Fig. 6.11 demonstrates scoping issues with global variables, automatic local variables and static local variables. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 80. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 81. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 82. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 83. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 84.  To understand how C++ performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack.  Analogous to a pile of dishes. ◦ When a dish is placed on the pile, it’s normally placed at the top—referred to as pushing. ◦ Similarly, when a dish is removed from the pile, it’s normally removed from the top—referred to as popping.  Last-in, first-out (LIFO) data structures—the last item pushed (inserted) is the first item popped (removed). ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 85. Function-Call Stack  One of the most important mechanisms for computer science students to understand is the function call stack (or program execution stack). ◦ supports the function call/return mechanism. ◦ Also supports the creation, maintenance and destruction of each called function’s automatic variables. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 86. Stack Frames  Each function eventually must return control to the function that called it.  Each time a function calls another function, an entry is pushed onto the function call stack. ◦ This entry, called a stack frame or an activation record, contains the return address that the called function needs in order to return to the calling function. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 87. Automatic Local Variables and Stack Frames  When a function call returns, the stack frame for the function call is popped, and control transfers to the return address in the popped stack frame. Stack Overflow  The amount of memory in a computer is finite, so only a certain amount of memory can be used to store activation records on the function call stack.  If more function calls occur than can have their activation records stored on the function call stack, an a fatal error known as stack overflow occurs. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 88. Function Call Stack in Action  Now let’s consider how the call stack supports the operation of a square function called by main (Fig. 6.12).  First the operating system calls main—this pushes an activation record onto the stack (shown in Fig. 6.13).  The activation record tells main how to return to the operating system (i.e., transfer to return address R1) and contains the space for main’s automatic variable (i.e., a, which is initialized to 10). ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 89. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 90. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 91.  Function main—before returning to the operating system—now calls function square in line 12 of Fig. 6.12.  This causes a stack frame for square (lines 16–18) to be pushed onto the function call stack (Fig. 6.14).  This stack frame contains the return address that square needs to return to main (i.e., R2) and the memory for square’s automatic variable (i.e., x). ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 92. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 93.  After square calculates the square of its argument, it needs to return to main—and no longer needs the memory for its automatic variable x. So square’s stack frame is popped from the stack—giving square the return location in main (i.e., R2) and losing square’s automatic variable.  Figure 6.15 shows the function call stack after square’s activation record has been popped. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 94. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 95.  Two ways to pass arguments to functions in many programming languages are pass-by-value and pass-by- reference.  When an argument is passed by value, a copy of the argument’s value is made and passed (on the function call stack) to the called function. ◦ Changes to the copy do not affect the original variable’s value in the caller.  To specify a reference to a constant, place the const qualifier before the type specifier in the parameter declaration. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 96. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 97.  With pass-by-reference, the caller gives the called function the ability to access the caller’s data directly, and to modify that data.  A reference parameter is an alias for its corresponding argument in a function call.  To indicate that a function parameter is passed by reference, simply follow the parameter’s type in the function prototype by an ampersand (&); use the same convention when listing the parameter’s type in the function header.  Figure 6.17 compares pass-by-value and pass-by-reference with reference parameters. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 98. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 99. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 100. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 101. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 102. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 103.  References can also be used as aliases for other variables within a function.  Reference variables must be initialized in their declarations and cannot be reassigned as aliases to other variables.  Once a reference is declared as an alias for another variable, all operations supposedly performed on the alias are actually performed on the original variable. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 104.  To specify that a reference parameter should not be allowed to modify the corresponding argument, place the const qualifier before the type name in the parameter’s declaration.  string objects can be large, so they (and objects in general) should be passed to functions by reference. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 105. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 106.  Functions can return references, but this can be dangerous.  When returning a reference to a variable declared in the called function, the variable should be declared static in that function. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.
  • 107. ©1992-2017 by Pearson Education, Inc. All Rights Reserved.