Chapter 5 Functions

advertisement
Chapter 5 Functions
ITP 134 C++ Study Guide
Chapter 5 Functions
Instructions: Use this Study Guide to help you understand the important points of this chapter. See
the book for lots of great programming examples. Important concepts, keywords, statements and
functions are shown in emphasized font.
Code syntax and examples are shown in code font.
Games & Graphics in C++
2nd edition by Tony Gaddis
5.1 Introduction to Functions
5.2 void Functions
CONCEPT: A function is a group of statements that exist
within a program for the purpose of performing a
specific task.
CONCEPT: A void function performs a task and then
terminates. It does not return a value to the statement
that called it. (pg 169)
In this chapter, we will demonstrate how void functions
can be used to break down a long program into smaller,
manageable pieces. This approach is sometimes called
divide and conquer because a large program is divided
into several smaller programs which are easily solved.
(pg 168)
CONCEPT: The code for a function is called a function
definition. A function definition has two parts: a
header and a body. To execute a function you write a
statement that calls it. (pg 169)
CONCEPT: A large program can be broken up into
manageable functions that each performs a major task.
(pg 168)
A program that is broken into smaller units of code,
such as functions, is known as a modularized program.
Function Definition – Function Header – Function Body



Code reuse is writing code to perform a particular task
once and then using it in several different programs or
executing it when needed. (page 169)

Void Functions and Value-Returning Functions

There are two general types of functions: those that
return a value, and those that do not. In C++, a function
that does not return a value is known as a void function.
When you call a value-returning function, it executes
and then returns a value to the calling program. (pg
169)
Eaton Note:
Benefits of Using Functions (not in book)





Simpler code
Code reuse
Better testing
Faster development
Easier facilitation of teamwork
ITP 134 – Mrs. Eaton
To create a function you write the function
definition. This specifies what a function does,
but it does NOT cause the function to execute.
The first line in a function definition is known as
the function header. (page 169)
Notice that the function header does NOT end
with a semicolon
One or more statements inside the curly braces
{ } are the function body and are performed any
time the function is executed. (pg 170)
This is the general syntax of a void function
definition:
void functionName()
{
statement1;
statement2;
}
Calling a Function
To execute a function, you must call it. When a function
is called, the program jumps to the function definition
code and executes it. After the function ends, the
programs jumps back to the statement that called it and
continues executing with the very next statement. (pg
170) Notice that you do NOT write the word void in the
function call statement.
Chapter 5 Study Guide – 2nd Edition
Page 1
Chapter 5 Functions
5.3 Local Variables
The general form of a call statement is: (pg 170)
functionName();
Function Prototypes
A function prototype is a statement that declares the
existence of a function, but does not define the
function. It tells the C++ compiler that a particular
function exists, and the definition appears LATER in the
program. (pg 172-173).
Eaton note: This is also called a function declaration in
some books and languages.
CONCEPT: A local variable is declared inside function
and cannot be accessed by statements that are outside
the function. Different functions can have local
variables with the same names because the functions
cannot see inside other functions for the local variables.
(pg 174)
Scope and Local Variables
Scope describes the portion of the overall program
where a variable may be accessed. A local variable’s
scope begins at the variable’s declaration and ends at
the end of the function where the variable is declared.
(pg 175)
The general syntax of a function prototype is:
Duplicate Variable Names
void functionName();
You cannot have 2 different variables with the same
name inside the same function. (pg 176)
Eaton Note: Function Names
C++ rules for naming a function: (not in book)
5.4 Passing Arguments to Functions




Must be 1 word. Cannot contain spaces.
First character must be a-z, A-Z, or _
(underscore)
After first character can also be a number, but
cannot be a special character such as $.
Names are case sensitive.
Top-Down Design
CONCEPT: Programmers commonly use a technique
known as top-down design to break down a program
into functions that each performs a single task. (pg 173174)
Top-down design process (pg 173-174)
1. Break down the overall task into a series of
subtasks.
2. Determine if each subtask can be further
broken down into more subtasks. Repeat this
step until no more subtasks can be identified.
3. Once you identify all the subtasks, then write
the code.
CONCEPT: An argument is any piece of data passed
into a function when the function is called. A
parameter is a special variable that receives an
argument that is passed into a function. (pg 176)
Eaton Note: Remember that the calling statement
contains arguments. Mnemonic call, call, get into a fight
and argument. The function definition contains
parameters.
Parameter Variable Scope
A parameter variable’s scope is the entire function
where the parameter is declared. It is visible (and
accessible) only to statements inside the function.
Eaton Note: In other words a parameter is a local
variable to the function definition. (pg 180)
Passing Multiple Arguments
If a function needs multiple arguments passed, you also
need multiple parameters to receive the values passed.
Eaton note: The arguments and parameters must be in
the same order and the same type. (pg 180)
Passing Arguments by Value
Eaton Note: Hierarchy Charts
When you pass an argument by value you pass only a
Programmers commonly use hierarchy charts to visually
copy of the value. Therefore if you change the value,
represent the relationships between functions in a
the value is only changed inside the function. (pg 182)
program. This is also known as a structure chart, and
Passing Arguments by Reference
shows boxes that represent each function. The boxes
When you pass an argument by reference, you pass the
are connected to show how the functions are called.
memory location of the value (not just a copy).
(not in book) These charts are really helpful for long
Therefore if you change the value, the value is changed
programs such as Chapter 8.7 Bug Zapper Game (pg
353-361)
ITP 134 – Mrs. Eaton
Chapter 5 Study Guide – 2nd Edition
Page 2
Chapter 5 Functions
inside the function and also in the calling program or
function. (pg 183)
5.5 Global Variables and Global Constants
CONCEPT: Global variables and global constants are
available to every function in a program, and is declared
outside all the functions in a program. (pg 184)
Global Variables
A global variable is a variable that is available to every
function in a program. Its scope is the entire program.
(pg 184) Be careful when using global variables:
 Global variables make debugging difficult.
 If you want to reuse a function, you will likely
need to redesign it so it does not rely on global
variables.
 Global variables make a program hard to
understand because the variable can be
modified by any statement in the entire
program.
Global Constants
This is the general format of a value returning function
definition (pg 189):
datatype functionName(ParameterList)
{
statement;
statement;
return expression;
}
Calling a Function
A function definition specifies what a function does, but
it does not cause the function to execute. To execute a
function, you must call it.
int x;
x = getCenterX;
Here is the function definition for the above:
int getCenterX()
{
//Calculate the center x coordinate.
int centerX = height/2;
// return value back to calling statement
return centerX;
A global constant is a named constant that is available
to every function in a program. Global constants are
typically used to represent unchanging values that are
needed throughout a program. Since it is constant it
cannot be changed. (pg 186)
}
5.6 Value-Returning Functions
You can use a shortcut to create the return statement
above. An alternative is
CONCEPT: A value-returning function is a function that
returns a value to statement of the program that called
it. (pg 188)
Writing Your Own Value-Returning Functions
You write a value-returning function the same way you
write a void function with two exceptions:
 You must specify a data type for the function.
 A value returning function must have a return
statement.
Note the datatype of the definition must match the
datatype of the variable in the calling statement (shown
in yellow above.)
Eaton Note: Making the Most of the Return Statement
int getCenterX()
{
return width/ 2;
}
Bool Functions
You can write Boolean function in C++ to return the
bool values true or false. You can use a Boolean
function to test a condition, and then return either true
or false to indicate whether or not the condition exists.
(pg 194) See code snippet on page 192 for an example.
Returning a String from a Function
You can write functions that return data of any type.
You can return strings. See Program 5-12
ReturnString.cpp (pg 195-196) for an example.
ITP 134 – Mrs. Eaton
Chapter 5 Study Guide – 2nd Edition
Page 3
Chapter 5 Functions
5.7 Calling string Class Member Functions
CONCEPT: A member function is a special function that
operates on an object. (pg 197)
The string class has several functions that are already
created in the string library that you can use such as
length. We will learn more about using member
functions in Chapter 11 Object-Oriented Programming
Chapter 5 Program Examples











ITP 134 – Mrs. Eaton
Chapter 5 Study Guide – 2nd Edition
Program 5-1 SimpleFunction .cpp (pg 171)
Program 5-2 FunctionPrototype .cpp (pg 173)
Program 5-3 PassArgument .cpp (pg 177)
Program 5-4 PassVariable.cpp (pg 178-9)
Program 5-5 TwoArgs .cpp (pg 180)
Program 5-6 PassByValue .cpp (pg 182)
Program 5-7 PassByReference .cpp (pg 183-4)
Program 5-8 GlobalVariable .cpp (pg 185)
In the Spotlight: Using Global Constants and
Reference Parameters
Program 5-9 Contributions .cpp (pg 186-188)
In the Spotlight: Writing Value-Returning
Functions
Program 5-11 CupsToOunces .cpp (pg 193-4)
Program 5-12 ReturnString .cpp (pg 195-6)
Page 4
Download