C++ Programming:
From Problem Analysis
to Program Design, Fourth Edition
Chapter 6: User-Defined Functions I
Introduction
• Functions (Modules) allow complicated
programs to be divided into manageable
pieces
• Some advantages of functions:
– A programmer can focus on just that part of
the program and construct it, debug it, and
perfect it
– Different people can work on different
functions simultaneously
– Can be re-used (even in different programs)
– Enhance program readability
2
Predefined Functions
• In algebra, a function is defined as a rule or
correspondence between values, called the
function’s arguments, and the unique value of
the function associated with the arguments
– If f(x) = 2x + 5, then f(1) = 7,
f(2) = 9, and f(3) = 11
• 1, 2, and 3 are arguments
• 7, 9, and 11 are the corresponding values
3
Predefined Functions (continued)
• Some of the predefined mathematical functions
are:
sqrt(x)
pow(x, y)
floor(x)//the largest whole number that is less than or equal to x
• Predefined functions are organized into
separate libraries
• I/O functions are in iostream header
• Math functions are in cmath header
4
Predefined Functions (continued)
• pow(x,y) calculates xy
– pow(2, 3) = 8.0
– Returns a value of type double
– x and y are the parameters (or arguments)
• sqrt(x) calculates the nonnegative square
root of x, for x >= 0.0
– sqrt(2.25) is 1.5
– Type double
5
Predefined Functions (continued)
• The floor function floor(x) calculates
largest whole number not greater (less than
or equal) than x
– floor(48.79) is 48.0
– Type double
– Has only one parameter
6
Predefined Functions (continued)
7
Predefined Functions (continued)
8
Predefined Functions (continued)
• Example 6-1 sample run:
User-Defined Functions
classified into two categories:
• Value-returning functions: have a return type
– Return a value of a specific data type using
the return statement
• Void functions: do not have a return type
– Do not use a return statement to return a
value
Chapter 6 discuses value-returning functions
Chapter 7 discuses void functions
Value-Returning Functions
• To use predefined functions you must:
– Include the appropriate header file in your
program using the include statement
– Know the following items:
• Name of the function
• Number of parameters, if any
• Data type of each parameter
• Data type of the value returned: called the type
of the function
Value-Returning Functions
(continued)
• Because the value returned by a value-returning function
is unique, we can:
– Save the value for further calculation
– Use the value in some calculation
– Print the value
• A value-returning function is used in an assignment or in
an output statement
• For user-defined functions, one more thing is associated
with functions in addition to 4 previous points:
– The code required to accomplish the task
Value-Returning Functions
(continued)
Value-Returning Functions
(continued)
• Heading: first four properties above
– Example: int abs(int number)
• Formal Parameter: variable declared in the
heading
– Example: number
• Actual Parameter: variable or expression
listed in a call to a function
– Example: x = pow(u, v)
Syntax: Value-Returning Function
• Syntax:
• functionType is also called the data type
or return type
Syntax: Formal Parameter List
Function Call
Syntax: Actual Parameter List
• The syntax of the actual parameter list is:
• Formal parameter list can be empty:
• A call to a value-returning function with an
empty formal parameter list is:
• A function’s formal parameter list can be
empty, thus the actual parameter list is also
empty.
• The number of actual parameters together
with their data types must match with the
formal parameter. (one to one
correspondence)
20
return Statement
• Once a value-returning function computes the
value, the function returns this value via the
return statement
– It passes this value outside the function via the
return statement
• The data type of return value must match the
function type.
• The return statement has the following
syntax:
Syntax: return Statement
In C++, return is a reserved word.
• When a return statement executes
– Function immediately terminates
– Control goes back to the caller
• The function call statement is replaced by the
returned value.
• When a return statement executes in the
function main, the program terminates
• In a function call, you specify only the actual
parameter, not it’s data type.
Function Prototype
• Function prototype: function heading without the body
of the function, put before function main,
• Syntax:
• It is not necessary to specify the variable name in the
parameter list
• The data type of each parameter must be specified
• Return st returns only one value, if it contains more
than one Exp, only the value of the last Exp is
returned. Ex: return x, y; returns only y.
Function Prototype (continued)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 28
int sum (int a, int b, int c);
int main ()
{
int x=1 , y=8, z=-4, r;
r=sum(x,y,z);
cout<<sum(x+2, y-4, 5)<<endl;
cout<<sum(11 , sum(r, 2, 3) , x*3)<<endl;
return 0;
}
int sum (int a, int b, int c)
{
int s;
s=a+b+c;
return s;
} 29
30
31
Flow of Execution
• Execution always begins at the first statement
in the function main
• Other functions are executed only when they
are called
• Function prototypes appear before any
function definition
– The program is compiled sequentially.
• The compiler can then correctly translate a
function call
Flow of Execution (continued)
• A function call results in transfer of control to
the first statement in the body of the called
function
• After the last statement of a function is
executed, control is passed back to the point
immediately following the function call
• After executing the function the returned
value replaces the function call statement
Programming Example: Largest
Number
• The function larger is used to determine the
largest number from a set of numbers
• Program determines the largest number from
a set of 10 numbers
• Input: a set of 10 numbers
• Output: the largest of 10 numbers
Programming Example: Program
Analysis
• Suppose that the input data is:
15 20 7 8 28 21 43 12 35 3
• Read the first number of the data set
– Because this is the only number read to this
point, you may assume that it is the largest
number so far and call it max
• Read the second number and call it num
– Compare max and num, and store the larger
number into max
Programming Example: Program
Analysis (continued)
• Now max contains the larger of the first two
numbers
• Read the third number and compare it with
max and store the larger number into max
– max contains the largest of the first three
numbers
• Read the next number, compare it with max,
and store the larger into max
• Repeat this process for each remaining
number in the data set
Programming Example: Algorithm
Design
• Read the first number
– Because this is the only number that you have
read, it is the largest number so far
– Save it in a variable called max
• For each remaining number in the list
– Read the next number
– Store it in a variable called num
– Compare num and max
Programming Example: Algorithm
Design (continued)
• For each remaining number in the list
(continued)
– If max < num
• num is the new largest number
• update the value of max by copying num into max
– If max >= num, discard num; that is, do
nothing
• Because max now contains the largest
number, print it
Summary
• Functions (modules) are miniature programs
– Divide a program into manageable tasks
• C++ provides the standard functions
• Two types of user-defined functions: value-
returning functions and void functions
• Variables defined in a function heading are
called formal parameters
• Expressions, variables, or constant values in
a function call are called actual parameters
Summary (continued)
• In a function call, the number of actual
parameters and their types must match with
the formal parameters in the order given
• To call a function, use its name together with
the actual parameter list
• Function heading and the body of the function
are called the definition of the function
• If a function has no parameters, you need
empty parentheses in heading and call
• A value-returning function returns its value via
the return statement
Summary (continued)
• A prototype is the function heading without
the body of the function; prototypes end with
the semicolon
• Prototypes are placed before every function
definition, including main
• User-defined functions execute only when
they are called
• In a call statement, specify only the actual
parameters, not their data types