Chapter 06
Chapter 06
Tenth Edition
Chapter 6
Functions
6-2
Topics 1 of 2
Topics 2 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-4
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-6
Function Definition 1 of 2
• A definition includes
name: the name of the function. Function names follow
the same rules as variable names
parameter list: the variables that hold the values that are
passed to the function when it is called
body: the statements that perform the function’s task
return type: data type of the value the function returns to
the part of the program that called it
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-7
Function Definition 2 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-8
Function Header
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-9
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-10
Calling a Function 1 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-11
Calling a Function 2 of 2
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-12
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-13
Function Prototypes 2 of 2
There are multiple ways to notify the compiler
about a function before making a call to the
function:
– Place the function definition before the calling
function’s definition
– Use a function prototype (similar to the header of the
function)
▪ Header: void printHeading()
▪ Prototype: void printHeading();
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-14
Prototype Notes
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-15
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-16
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-17
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-18
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Calling Functions with
6-19
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-20
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-21
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-22
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-23
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-25
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-26
statements
A function may calculate a return value and use a
single return statement. The previous example
could be written as:
bool isValid(int val) // header
{
bool result;
int min = 0, max = 100;
if (val >= min && val <= max)
result = true;
else
result = false;
return result; // single return
}
…
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6.9 Using Functions in a Menu-Driven
6-28
Program
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
Screen Management in a Menu-Driven
6-29
Program
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-32
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-33
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-34
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-35
Global Constants
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-36
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-37
• Local variables
– Only exist while the function is executing
– Are redefined each time function is called
– Lose their contents when function terminates
• static local variables
– Are defined with key word static
static int counter;
– Are defined and initialized only the first time the function
is executed
– Retain their values between function calls
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-38
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-39
Default Arguments 2 of 2
• If not all parameters to a function have default
values, the ones without defaults must be declared
first in the parameter list
int getSum(int, int=0, int=0);// OK
int getSum(int, int=0, int); // wrong!
• When an argument is omitted from a function call,
all arguments after it must also be omitted
sum = getSum(num1, num2); // OK
sum = getSum(num1, , num3); // wrong!
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-40
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-41
Reference Variables
• A reference variable is an alias for another
variable
• When used as a function parameter, it is
defined with an ampersand (&) in the
prototype and in the header
void getDimensions(int&, int&);
• Changes made to a reference variable are
made to the variable it refers to
• Use reference variables to implement passing
parameters by reference
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-42
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-43
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-45
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
exit() – Passing Values to Operating
6-47
System
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved
6-48
Copyright
Copyright © 2020, 2017, 2014 Pearson Education, Inc. All Rights Reserved