Chapter 4
void Functions
Starting Out with
Games & Graphics in C++
Tony Gaddis
Addison Wesley
is an imprint of
© 2010 Pearson Addison-Wesley. All rights reserved.
4.1 Modularizing a Program with Functions
Concept:
A large program can be broken up
into manageable functions that
each performs a major task.
Copyright © 2010 Pearson Addison-Wesley
1-2
4.1 Modularizing a Program with Functions
• A function is a group of programming statements
that has a name
• There are two general types of functions:
– Those that return a value
– Known as value-returning functions
• For example
– dbRND
– Those that do not
– Known as void functions
• For example
– dbDot
– dbLine
– dbBox
Copyright © 2010 Pearson Addison-Wesley
1-3
4.1 Modularizing a Program with Functions
• Functions can be used
to break a program
down into smaller,
manageable pieces
• This approach is
sometimes called
divide and conquer
Figure 4-1 Using functions to divide
and conquer a large task
Copyright © 2010 Pearson Addison-Wesley
1-4
4.1 Modularizing a Program with Functions
Benefits of Using Functions
• Simpler Code
– A program’s code tends to be simpler and easier to understand when it is
broken down into functions
• Code Reuse
– Functions reduce the duplication of code within a program
• Better Testing
– When each task within a program is contained in its own function, testing
and debugging becomes simpler
• Faster Development
– Functions can be written for commonly needed tasks, and those functions
can be incorporated into each program that needs them
• Easier Facilitation of Teamwork
– Different programmers can be assigned the task of writing different
functions
Copyright © 2010 Pearson Addison-Wesley
1-5
4.2 Defining and Calling a void Function
Concept:
The code for a function is known
as a function definition. To
execute a function you write a
statement that calls it.
Copyright © 2010 Pearson Addison-Wesley
1-6
4.2 Defining and Calling a void Function
Function Names
• When naming a function, C++ requires you follow the
same rules for naming variables, which we recap here:
– Function names must be
• One word
• No spaces
– First character must be
• Letter a – z, A – Z, or underscore (_)
– After first character
• Letters a – z, A – Z, numbers 0 – 9, or underscore(_)
– Uppercase and lowercase characters are distinct
• LineLength is not the same as linelength
Copyright © 2010 Pearson Addison-Wesley
1-7
4.2 Defining and Calling a void Function
Function Names
• Because functions perform actions, programmers
commonly use verbs in function names
– For example
• drawSmileyFace
• setDrawingColor
• displayCaption
• clearScreen
• Function names should describe what the function
does
Copyright © 2010 Pearson Addison-Wesley
1-8
4.2 Defining and Calling a void Function
Defining a void Function
• To create a function you write its definition
• Here is the general format of a void function
definition:
Copyright © 2010 Pearson Addison-Wesley
1-9
4.2 Defining and Calling a void Function
Defining a void Function
• The first line is known as
the function header
– It marks the beginning of
the function
• The header for a void
function begins with the
keyword void
• Followed by the function
name
• Followed by a set of
parentheses
– Notice that the function
header does not end with a
semicolon
Copyright © 2010 Pearson Addison-Wesley
1-10
4.2 Defining and Calling a void Function
Defining a void Function
• After the function
header, a set of
statements enclosed in
curly braces appears
– This set of statements is
known as the body of
the function
– These are the
statements that execute
when the function is
called
Copyright © 2010 Pearson Addison-Wesley
1-11
4.2 Defining and Calling a void Function
Defining a void Function
• Let’s look at an example of a void function definition:
• This code defines a function named displayMessage
• The body of the function contains two statements
• Notice the statements are indented, this is not required, but makes
the code easier for people to read
Copyright © 2010 Pearson Addison-Wesley
1-12
4.2 Defining and Calling a void Function
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
• This is how we would call the displayMessage
function:
Copyright © 2010 Pearson Addison-Wesley
1-13
4.2 Defining and Calling a void Function
Calling a Function
• To call a function, you
– Write its name
– Followed by a set of
parentheses
– Followed by a semicolon
• Notice that you do not
write the word void in
the function call
statement
Copyright © 2010 Pearson Addison-Wesley
1-14
4.2 Defining and Calling a void Function
Calling a Function
•
When a function is called
– the program jumps to that function
– executes the statements inside it
– Programmers commonly say that
control of the program transfers to the
function
– This simply means that the function
takes control of the programs
execution.
Figure 4-3 Calling the
displayMessage function
Copyright © 2010 Pearson Addison-Wesley
1-15
4.2 Defining and Calling a void Function
Calling a Function
•
When the end of a function is reached
– the program jumps back to the statement
that called the function
– execution resumes with the very next
statement
– When this happens, we say that the
function returns
Figure 4-4 The displayMessage
function returns
Copyright © 2010 Pearson Addison-Wesley
1-16
4.2 Defining and Calling a void Function
Function Prototypes
• In C++, a function’s definition must appear in
the file before any statements that call the
function, or a compiler error will occur
• The only exception is when you use function
prototypes
• A function prototype is a statement that
declares the existence of a function, but does
not define the function
Copyright © 2010 Pearson Addison-Wesley
1-17
4.2 Defining and Calling a void Function
Function Prototypes
• A function prototype is
– Merely a way of telling the compiler that a particular
function exists in the program, and its definition appears
at a later point
• This is how function prototypes for the
displayMessage and pauseProgram functions
would look:
Copyright © 2010 Pearson Addison-Wesley
1-18
4.2 Defining and Calling a void Function
Function Prototypes
• Look like function headers, except a
semicolon appears at the end
• Are usually placed near the top of a program
so the compiler will encounter them before
any function calls
• Are used by most C++ programmers
• Allow you to write the DarkGDK function at the
top of your source code file
Copyright © 2010 Pearson Addison-Wesley
1-19
4.2 Defining and Calling a void Function
Function Prototypes
Function Prototypes
Function Calls
Function Definitions
Copyright © 2010 Pearson Addison-Wesley
1-20
4.3 Designing a Program to Use Functions
Concept:
Programmers commonly use a
technique known as top-down
design to break down a program
into functions that each perform a
single task.
Copyright © 2010 Pearson Addison-Wesley
1-21
4.3 Designing a Program to Use Functions
• Programmers commonly use a technique known as
top-down design to break a program down into
functions
• The process of top-down design is performed in the
following manner:
– Overall task is broken down into a series of subtasks
– Subtasks are examined and are broken down into more
subtasks
• This step is repeated until no more subtasks can be identified
– Subtasks are written into code
• This process is called top-down design because the
programmer begins by looking at the topmost level of
tasks that must be performed, and then breaks down
those tasks into lower levels of subtasks
Copyright © 2010 Pearson Addison-Wesley
1-22
4.3 Designing a Program to Use Functions
Hierarchy Charts
• A hierarchy chart, which is also known as a structure chart,
shows boxes that visually represent each function in a program
Figure 4-8 Hierarchy chart
Copyright © 2010 Pearson Addison-Wesley
1-23
4.4 Local Variables
Concept:
A local variable is declared inside a
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 each
other’s variables.
Copyright © 2010 Pearson Addison-Wesley
1-24
4.4 Local Variables
• A variable that is declared inside a function is
called a local variable
• A local variable
– Belongs to the function in which it is declared
– Can only be accessed by statements inside the
function in which it is declared
• The term local is meant to indicate that the
variable can be used only locally, within the
function in which it is declared
Copyright © 2010 Pearson Addison-Wesley
1-25
4.4 Local Variables
•
An error will occur if a statement in
one function tries to access a local
variable that belongs to another
function
DarkGDK function local variables
not visible to drawBlueBox function
Copyright © 2010 Pearson Addison-Wesley
1-26
4.4 Local Variables
Scope and Local Variables
• Programmers commonly use the term scope to describe the
part of a program in which a variable may be accessed
• A variable is visible only to statements inside the variable’s
scope
• A local variable’s scope
– Begins at the variable’s declaration
– Ends at the end of the function in which the variable is declared
• A local variable cannot be accessed by statements that are
– Outside the function
– Inside the function, but before the variable’s declaration
Copyright © 2010 Pearson Addison-Wesley
1-27
4.4 Local Variables
Duplicate Variable Names
• You cannot have two variables with the
same name in the same scope
• A function cannot have two local variables
with the same name
• You can have local variables with the same
name declared in separate functions
Copyright © 2010 Pearson Addison-Wesley
1-28
4.5 Passing Arguments to Functions
Concept:
An argument is a piece of data
that is passed into a function when
the function is called. When an
argument is passed into a
function, a special variable in the
function known as a parameter
receives the argument.
Copyright © 2010 Pearson Addison-Wesley
1-29
4.5 Passing Arguments to Functions
• Pieces of data that are sent into a function are
known as arguments
– The function can use its arguments in calculations or
other operations
• If you want a function to receive arguments when it
is called
– you must equip the function with one or more
parameter variables
• A parameter variable is a special variable that is
assigned the value of an argument when a function
is called
– A parameter variable is often simply called a
parameter
Copyright © 2010 Pearson Addison-Wesley
1-30
4.5 Passing Arguments to Functions
• Let’s look at an example of a function that uses a parameter
variable to accept an argument:
– When you call the function, you pass an argument indicating the
circle’s radius
Parameter variable declaration
• The parameter variable’s
– Declaration is inside the
parentheses of the
function header
– Purpose is to receive an
integer argument when
the function is called
Copyright © 2010 Pearson Addison-Wesley
1-31
4.5 Passing Arguments to Functions
Function prototype
Function call
Function definition
Copyright © 2010 Pearson Addison-Wesley
1-32
4.5 Passing Arguments to Functions
• Notice the function prototype for the drawCircle
function does not list the name of the parameter
variable
• It is not necessary to list the name of the parameter
variable inside the parentheses
• Only the data type is required
• Listing a parameter name will not cause an error
• The compiler merely ignores parameter name
Copyright © 2010 Pearson Addison-Wesley
1-33
4.5 Passing Arguments to Functions
• When the drawCircle
function is called
– The number 100 appears
inside the parentheses
• 100 is the argument that
is being passed to the
function
– When the function
executes, 100 will be
assigned to radius
Figure 4-10 the argument 100 is assigned to the
radius function
Copyright © 2010 Pearson Addison-Wesley
1-34
4.5 Passing Arguments to Functions
Parameter Variable Scope
• Earlier in this section, you learned
– A variable’s scope is the part of the program in which
the variable may be accessed
– A variable is visible only to statements inside the
variable’s scope
• A parameter variable’s scope is the entire function
in which the parameter is declared
• It is visible only to statements inside the function
Copyright © 2010 Pearson Addison-Wesley
1-35
4.5 Passing Arguments to Functions
Passing Multiple Arguments
•
•
•
•
•
If a function needs multiple
arguments
You can equip it with multiple
parameter variables
Notice, two parameter variables,
num1 and num2 are declared
inside the parentheses
This is often referred to as a
parameter list
Also notice a comma separates
the declarations
Figure 4-13 Two arguments passed
into two parameters
Copyright © 2010 Pearson Addison-Wesley
1-36
4.5 Passing Arguments to Functions
Passing Arguments by Value
• All of the programs we have looked at so far pass
arguments by value
• Arguments and parameter variables are separate items
in memory
• Passing an argument by value means that only a copy
of the argument’s value is passed into the parameter
variable
• If the parameter variable is changed inside the function
– It has no effect on the argument in the calling part of the
program
Copyright © 2010 Pearson Addison-Wesley
1-37
4.5 Passing Arguments to Functions
Passing Arguments by Value
Program 4-10 (PassByValue.cpp) partial listing
The changeMe function can only modify a copy of the number
variable, not the number variable itself
Copyright © 2010 Pearson Addison-Wesley
1-38
4.5 Passing Arguments to Functions
Passing Arguments by Value
• Passing an argument is a way
that one function can
communicate with another
function
• When the argument is passed
by value:
– The communication works in one
direction
– The calling function can use the
argument to communicate with
the called function
– The called function cannot use
the argument to communicate
with the calling function
Copyright © 2010 Pearson Addison-Wesley
Figure 4-15 Example output of
Program 4-10
1-39
4.5 Passing Arguments to Functions
Passing Arguments by Reference
• Passing an argument by reference means that the
argument is passed into a special type of
parameter known as a reference variable
• A reference variable allows the argument to be
modified in the calling function
• A reference variable acts as an alias for the
variable that was passed as an argument
• A reference variable references the other variable
• Anything you do to a reference variable is actually
done to the variable it references
Copyright © 2010 Pearson Addison-Wesley
1-40
4.5 Passing Arguments to Functions
Passing Arguments by Reference
• Reference variables are useful for establishing two-way
communication between functions
• Communication can take place in the following ways:
– The calling function can communicate with the called function
• by passing an argument
– The called function can communicate with the calling function
• by modifying the value of the argument via the reference variable
Copyright © 2010 Pearson Addison-Wesley
1-41
4.5 Passing Arguments to Functions
Passing Arguments by Reference
• In C++, you declare a reference variable by placing an
ampersand (&) before the variable’s name
• For example, look at the following function:
•
•
•
Inside the parentheses, the & indicates that value is a reference variable
The function assigns 0 to the value parameter
Because value is a reference variable, this action is actually performed on
the variable that was passed to the function as an argument
Copyright © 2010 Pearson Addison-Wesley
1-42
4.5 Passing Arguments to Functions
Passing Arguments by Reference
• When using a reference parameter variable
– Include the ampersand (&) after the data type in the
function prototype
• Here is the prototype for the setToZero function:
Copyright © 2010 Pearson Addison-Wesley
1-43
4.5 Passing Arguments to Functions
Passing Arguments by Reference
Function prototype
Variable declarations
Function Calls
Function Definition
Copyright © 2010 Pearson Addison-Wesley
1-44
4.5 Passing Arguments to Functions
Passing Arguments by Reference
• Variables a, b, and c
are initialized with the
value 99
• Those variables are
passed as arguments to
the setToZero function
• Each time the function is
called, the variable that
is passed as an
argument is set to 0
Copyright © 2010 Pearson Addison-Wesley
Figure 4-16 Output of Program 4-11
1-45
4.5 Passing Arguments to Functions
Passing Arguments by Reference
Copyright © 2010 Pearson Addison-Wesley
1-46
4.6 Global Variables and Constants
Concept:
A global variable is accessible to all
the functions in a program.
Copyright © 2010 Pearson Addison-Wesley
1-47
4.6 Global Variables and Constants
• A global variable is declared outside of all
the functions in a program
• A global variable’s scope
– Begins at the variable’s declaration
– Ends at the end of the program
• A global variable can be accessed by all
functions that are defined after the variable
is declared
Copyright © 2010 Pearson Addison-Wesley
1-48
4.6 Global Variables and Constants
Global variable declaration
Accessed by all functions
Copyright © 2010 Pearson Addison-Wesley
1-49
4.6 Global Variables and Constants
• You should be careful when using global variables
• Global variables can:
– Make debugging difficult
• Any statement can change the value
• Programs can contain thousands of statements
– Limit code reusability
• Functions that use global variables are dependent on those
values and must be redesigned before they can be used in
another program
– Make a program hard to understand
• You must be aware of all the parts that access the variable
• With global variables, this can be the entire program
Copyright © 2010 Pearson Addison-Wesley
1-50
4.6 Global Variables and Constants
Global Constants
• A global constant is a named constant that is
available to every function in the program
• A global constant:
– Contains a value that cannot be changed during
the program’s execution
– Reduces many of the potential hazards that are
associated with global variables
– Can be used to represent unchanging values that
are needed throughout the program
Copyright © 2010 Pearson Addison-Wesley
1-51
4.6 Global Variables and Constants
Global Constants
Global constant declarations
Copyright © 2010 Pearson Addison-Wesley
1-52
4.6 Global Variables and Constants
Global Constants
Used in a function
And in another function
Copyright © 2010 Pearson Addison-Wesley
1-53
Chapter 4
void Functions
QUESTIONS
Addison Wesley
is an imprint of
?
© 2010 Pearson Addison-Wesley. All rights reserved.