Programming Languages Chapter 6 Programming From Problem Analysis Program Design Eighth Edition Userdefined Functions Guide

subject Type Homework Help
subject Pages 9
subject Words 4500
subject Authors D. S. Malik

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-1
Chapter 6
User-Defined Functions
A Guide to this Instructor’s Manual:
We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.
This document is organized chronologically, using the same headings that you see in the
textbook. Under the headings, you will find lecture notes that summarize the section, Teacher
Tips, Classroom Activities, and Lab Activities. Pay special attention to teaching tips and
activities geared towards quizzing your students and enhancing their critical thinking skills.
In addition to this Instructor’s Manual, our Instructor’s Resources also contain PowerPoint
Presentations, Test Banks, and other supplements to aid in your teaching experience.
At a Glance
Instructor’s Manual Table of Contents
Overview
Objectives
Teaching Tips
Quick Quizzes
Class Discussion Topics
Additional Projects
Additional Resources
Key Terms
page-pf2
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-2
Lecture Notes
Overview
Chapter 6 focuses on functions. First, students will learn more about the predefined
functions in C++. Then they will learn about the use of user-defined functions to break a
program into manageable pieces. Students will examine the mechanics of writing their
own functions, including how to use parameters and return values. They will also
examine reference parameters and learn how to use them in a program. The scope of
identifiers will be examined in depth, as well as the purpose and use of static variables.
Finally, students will be introduced to the concept of function overloading.
Objectives
In this chapter, the student will:
Learn about standard (predefined) functions and discover how to use them in a program
Learn about user-defined functions
Examine value-returning functions, including actual and formal parameters
Explore how to construct and use a value-returning, user-defined function in a program
Learn about function prototypes
Learn how to construct and use void functions in a program
Discover the difference between value and reference parameters
Explore reference parameters and value-returning functions
Learn about the scope of an identifier
Examine the difference between local and global identifiers
Discover static variables
Learn how to debug programs using drivers and stubs
Learn function overloading
Explore functions with default parameters
Teaching Tips
Introduction
1. Point out that only the main function has been used in the programming examples thus
far in the text.
2. Explain that functions are like building blocks (often called modules), which can be
combined to form a larger program.
Teaching
Tip
Spend some time discussing why the use of functions is beneficial to a program.
After all, the entire code of a program could be written in the main function.
Explain that functions enhance a program’s readability, improve the
page-pf3
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-3
Predefined Functions
1. Compare C++ predefined functions to algebraic functions. Use the functions in the
cmath library to illustrate the concept of a function as it relates to programming.
2. Review some of the C++ header files and predefined functions listed in Table 6-1.
3. Illustrate the use of C++ predefined functions using Example 6-1.
Teaching
Tip
Although your students have used C++ functions in previous chapters,
organizing their own programs into functions may be a new concept to them. Use
the predefined functions presented in this section to introduce the concept of
encapsulation. Emphasize the “black box” nature of a function. Explain that
although we need to know how to call a C++ function, we do not need to know
anything about its implementation. When we write our own functions, we will
call them in a similar fashion, although we may be aware of the implementation
of the function.
Quick Quiz 1
1. How many parameters are in the C++ predefined function pow?
2. The C++ function sqrt is of type ____________________.
3. To use predefined functions in a program, you must include the
____________________ that includes the function specification.
4. The C++ function toupper returns a(n) ____________________ value.
development process by isolating code, and allow several people to work on
different portions of a program.
page-pf4
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-4
User-Defined Functions
Teaching
Tip
This is also a good opportunity to visit the concept of reusability. Emphasize that
functions allow programmers to write code once and then reuse it as necessary.
The C++ predefined functions are an example of this concept.
Value-Returning Functions
1. Briefly describe the items a programmer needs to know when using a predefined
2. Discuss how the return value of a function is used in a program.
Teaching
Tip
Use code snippets from the text to demonstrate how a function call can be used
in both an assignment statement and an output statement.
4. Define the terms actual parameter and formal parameter and explain the difference
between the two.
Teaching
Tip
Spend some extra time discussing actual and formal parameters. These terms are
often used incorrectly or interchangeably. Verify that students understand that
formal parameters are placed in a function header as a definition, somewhat like
placeholders. Actual parameters are used when a program passes required values
to a function during a function call.
Syntax: Value-Returning Function
1. Explain the syntax of a value-returning function.
page-pf5
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-5
Teaching
Tip
Emphasize that a value-returning function only includes the data type of the
value that the function returns in the function header, not in the parameter list.
Syntax: Formal Parameter List
1. Describe the syntax of a formal parameter list.
Teaching
Tip
Understanding how functions work is critical to successful programming, so it
might be wise to revisit basic function concepts throughout this chapter. In this
case, reiterate that parameter lists are used because functions often need input
values. Provide an example from the text.
Function Call
1. Briefly note the syntax used to call a function.
Syntax: Actual Parameter List
1. Step through the syntax of an actual parameter list.
2. Discuss the rules involving actual parameter lists, including matching data types and a
one-to-one correspondence with the formal parameter list. Note that the parameter list
can be empty, but the function call will still require parentheses after the function name.
Teaching
Tip
Stress the fact that the actual parameter list must match the formal parameter list
in number, type, and order. Give some examples to reinforce this point.
Quick Quiz 2
1. A value-returning function is used (called) in a(n) ____________________.
2. True or False: void functions do not have a return type.
3. The code required to accomplish a function’s task is called the ____________________
of the function.
4. True or False: An actual parameter is a variable declared in the function heading.
page-pf6
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-6
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
return Statement
1. Explain the purpose of the return statement in a function.
Syntax: return Statement
1. Note the syntax of the return statement and describe its execution in detail.
2. Use the examples in this section to discuss different approaches of returning a value
from a function.
3. Point out that a return statement causes a function to terminate, and control passes
back to the caller of the function.
Teaching
Tip
Call attention to the compareThree function in this section to demonstrate
how a function can be used as a parameter. Note that this functionality is often
used in programming.
Function Prototype
1. Define the term function prototype and explain how it is used in C++ programs to avoid
compilation errors. Explain why a function prototype may be needed.
Syntax: Function Prototype
2. Discuss the example in the “Note” to reiterate that a value-returning function must
return a value, and the data type of the value must match the function type.
Value-Returning Functions: Some Peculiarities
1. Use the example of the secret function versions listed on page 363 to illustrate the
More Examples of Value-Returning Functions
1. Review Examples 6-3 through 6-7.
Flow of Compilation and Execution
page-pf7
1. Describe in detail the flow of execution in a program with functions. Review the use of
2. Point out that the first statement in main is always the first line that is executed,
regardless of where main appears in the program.
3. Step through the “Largest Number” Programming Example to consolidate the concepts
Quick Quiz 3
1. What is a local declaration?
2. True or False: When a return statement executes in a function, the function
immediately terminates and the control goes back to the caller.
3. True or False: A return statement must be the last statement in a function.
4. A(n) ____________________ is a function heading without the body of the function.
Void Functions
1. Explain the similarities and differences between void and value-returning functions.
2. Point out that although void functions do not return a value, they can have a return
statement in order to exit the function.
Teaching
Tip
This chapter moves rapidly through some fairly complex concepts regarding
parameter passing. Make sure that students do not feel lost by asking questions
periodically as you work through the chapter. Start by verifying that students
understand why a call to a void function is a stand-alone statement.
page-pf8
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-8
Teaching
Tip
Note that although void functions do not return a value and may not even have
parameters, they are still useful in organizing a program so that it is more
modular. This creates cleaner code that is easier to follow, thus making later
modifications easier.
4. Define the terms value parameter and reference parameter. Introduce the syntax for
5. Step through Examples 6-10 and 6-11 to illustrate the use of a void function with
parameters.
Teaching
Tip
Although reference parameters will be discussed at length later in this chapter,
verify that students understand the basic concept of a reference parameter. Using
C++ reference parameters is tricky for beginning programmers; most students
will need some additional time to process this information.
Quick Quiz 4
1. What is the syntax of a void function header?
2. What is the syntax of a void function call?
3. True or False: No information can be passed in and out of void functions without
parameters.
4. What is a reference parameter?
Value Parameters
1. Discuss how a function call operates in terms of how the values in the actual parameters
are transferred to the formal parameters. Use Example 6-12 to illustrate this process.
page-pf9
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-9
Teaching
Tip
Emphasize that formal parameters retain their own copy of the value that is
passed from the actual parameter. This is a difficult concept to grasp, so you may
want to describe the process by drawing a simple diagram in addition to the ones
that are provided with the programs in this chapter. Reiterate that value
parameters work in a one-way direction, meaning that information from the
formal parameter cannot be passed back to the calling program, or actual
parameter.
Reference Variables as Parameters
2. Discuss the situations in which using reference parameters is useful.
Calculate Grade (Example 6-13)
2. Note that the & operator is used to signify a reference parameter. Use Figures 6-5
Value and Reference Parameters and Memory Allocation
1. Review the concept of local variables.
2. Remind students that value parameters receive a copy of the actual parameter data,
while reference parameters receive the memory address of the actual parameter.
Teaching
Tip
This section discusses in detail how memory is manipulated with respect to both
value and reference parameters. Two programs are presented with different
scenarios and with figures that demonstrate how the parameters are (Examples 6-
14 and 6-15, and the accompanying figures). Encourage students to study the
programs more on their own. Alternately, divide the students into groups, and
have them walk through one of the programs together. Then, ask them to present
the key issues regarding parameter passing in the program they reviewed.
page-pfa
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-10
Reference Parameters and Value-Returning Functions
1. Note that you can use reference parameters in a value-returning function, but this
2. Explain that if a function needs to return more than one value, one should change it to a
void function and use the appropriate reference parameters to return the values.
Scope of an Identifier
2. Explain the difference between a local identifier and a global identifier.
4. Step through the program in this section and use Table 6-2 to discuss the visibility of
the identifiers in the program.
5. Describe the purpose of the scope resolution operator.
6. Describe the purpose of the reserved word extern.
Teaching
Tip
Note that global variables would not typically be declared between function
definitions, as they are in the program in this section. Discuss good
programming practices with respect to declaring variable and function
prototypes before main. Briefly discuss the typical use of the word extern for
accessing variables in another source file.
Quick Quiz 5
1. A(n) ____________________ identifier is declared outside of every function definition.
2. Define the scope of an identifier.
page-pfb
3. True or False: The scope of a function name is the same as the scope of a global
identifier.
Global Variables, Named Constants, and Side Effects
1. Discuss the side effects associated with using global variables in a program.
2. Review why named constants are typically declared as global variables.
3. Walk through Example 6-17 (Menu-Driven Program).
Teaching
Tip
This chapter mentions that global variables can cause problems because they can
be modified anywhere in the program. Discuss specific situations in which this
might be an issue, for example, when alternating function calls in which the same
global variable is used.
Static and Automatic Variables
1. Explain the difference between an automatic variable and a static variable.
2. Discuss the syntax, scope, and initialization of static variables.
3. Use Example 6-18 to illustrate a typical use of a static variable.
Teaching
Tip
Note that static and global variables are both kept in memory as long as the
program executes. Explain why using a local static variable is usually preferable
to using a global variable.
Debugging: Using Drivers and Stubs
1. Define the term driver program to mean a program that is used to test a function.
2. Note that at times it is not possible to test one function alone because it depends on the
results of another function such as in the program shown on pages 413-414. In this case,
page-pfc
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-12
Function Overloading: An Introduction
2. Explain that when two or more functions have the same name, the compiler determines
the correct function by its parameter list.
3. Introduce the term signature.
Teaching
Tip
Overloading is an important concept. You have probably introduced some form
of overloading in previous chapters, so you might want to revisit some of these
examples, such as operator overloading with the equality operator. Ask students
if they find the function/operator overloading in the C++ libraries useful, and if
so, why.
Functions with Default Parameters
1. Discuss the rules regarding the use of default parameters.
2. Emphasize that calls to functions with default parameters do not necessarily have to
3. Illustrate the use of default parameters with Example 6-19.
4. Encourage students to study and execute the Programming Examples (“Classify
Class Discussion Topics
1. This chapter mentioned that function prototypes are useful for avoiding compilation
errors. Are there any other advantages of using function prototypes?
2. When designing the placement of a return statement(s) in a function, why is it a good
idea to return a value as soon as it is computed?
3. Briefly discuss how local declarations are stored in computer memory. Are there any
reasons to avoid using local declarations if it is possible to achieve the same result
without them?
page-pfd
4. If reference parameters can be used in any function, why use value parameters at all?
How are value parameters useful in processing data in a program?
5. Why is it generally not wise to combine value-returning functions with reference
parameters?
Additional Projects
1. Write a program in which you implement two math functions and call them from the
main function with a variety of test data. You may implement your own version of any
2. Write a program that includes a function called displayTime() to output the current
time. You will call the predefined function ctime() in your function. Do some
3. Write a program that simulates a questionnaire to find a roommate. You will ask the
user various questions that might determine a suitable match, such as their age, gender,
smoking preference, sleeping habits, pets, etc. Use an overloaded function, called
match, to determine if their preferences match your own.
The match functions will take a string, Boolean, floating-point, or integer parameter
Additional Resources
1. C++ Notes: Functions:
2. C++ Notes: Function Reference Parameters:
page-pfe
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 6-14
Key Terms
Actual parameter: a variable or expression listed in a call to a function
Automatic variable: a variable for which memory is allocated at block entry and
deallocated at block exit
Different formal parameter lists: when two or more functions with the same name
have a different number of formal parameters, or if they have the same number of
parameters, the data type of the parameters differs in at least one position
Driver: a program that tests a function
External variable: a global variable declared within a function using the extern
reserved word; the keyword extern indicates that the variable is declared elsewhere
Local identifier: an identifier declared within a function (or block)
Local variables: variables declared in the body of a function (or block) for use only
within that function (or block)
Module: another name for a function
Nested block: a block declared within another block
variable
Signature (of a function): consists of the function name and its formal parameter list
Static variable: a variable for which memory remains allocated as long as the program
executes
Stub: a function that is not fully coded
page-pff
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.