This preview shows pages 1–3. Sign up to view the full content.
Chapter
Functions
CHAPTER
GOALS
.. To be able to program functions and procedures
.. To become familiar with the concept of parameter passing
.. To recognize when to use value and reference parameters
.. To appreciate the importance of function comments
.. To be able to determine the scope of variables
~
To minimize the use of side effects and global variables
.. To develop strategies for decomposing complex tasks into simpler ones
.. To document the responsibilities of functions and their callers with
preconditions
Functions are
a fundamenral
reused. In this chapter, you will learn
building block
of C++ programs. A
how to design and implement your own
function packages a computation in a
functions, and how to break up complex
form that can be easily understood and
tasks into sets
of cooperating functions.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 5.1
CHAPTERS
Functions
CHAPTER
CONTENTS
5.1 Functions as Black Boxes
154
5.6 Side Effects 168
5.2 Writing Functions
156
5.7 Procedures
169
171
172
Syntax 5.1: Function Definition
156
5.8 Reference Parameters
Productivity Hint 5.1: Write Functions with Reuse
Syntax
5.4:
Reference Parameter
in Mind
159
Advanced Topic 5.2: Constant References 173
5.3
Function Comments
159
Productivity Hint 5.2: Global Search and
5.9 Variable Scope and Global Variables 174
Replace
161
Quality TIp 5.2: Minimize Global Variables
176
176
Productivity Hint
5.3:
Regular Expressions
162
5.10 Stepwise Refinement
5.4 Return Values
162
Quality Tip
Keep Functions Short 178
179
185
Syntax 5.2: return Statement 163
5.11 From Pseudocode to Code
Common Error 5.1: Missing Return Value 165
5.12 Walkthroughs
5.5
Parameters 166
Productivity Hint 5.5: Empty Stubs 189
Comrnon Error 5.2: Type Mismatch
167
190
Function Declaration (or
Advanced Topic
5.1:
Function Declarations
167
Syntax 5.6: Assertion
Random Fact 5.1: The Explosive Growth
Prototype) 167
of Personal Computers
192
Syntax 5.5: Constant Reference Parameter 174
Parameters 165
Quality Tip 5.1: Use Meaningful Names for
Productivity Hint 5.4: Commenting Out a Section
of Code 188
5.13
Preconditions 190
Functions
as
Black
Box
s
You have used a number of functions that were provided with the C++ system library.
Examples are
sqrt Computes the square root of a floatingpoint number
getline
Reads a line from a stream
You probably don't know how these functions perform their job. For example, how does
sqrt compute square roots? By looking up values in a table? By repeated guessing of the
answer? You will actually learn in Chapter 6 how to compute square roots using nothing
more than basic arithmetic, but you don't need to know the internals of the computation
to use the square root function. You can think of sq
rt
as a
black box,
as shown in
Figure 1.
When you use sq
rt ex) inside rna in, the
input value
or
parameter value
x is transferred,
or
passed,
to the sqrt function. The execution of the
rnain
function is temporarily
Parameter
~
»
.
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 02/28/2008 for the course CSCI 455 taught by Professor Bono during the Spring '08 term at USC.
 Spring '08
 Bono

Click to edit the document details