C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6 User-Defined Functions I At a Glance Instructor’s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms 6-1 C++ Programming: Program Design Including Data Structures, Fourth 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. By the end of the chapter, students will be able to construct programs using both predefined and userdefined functions. 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 Teaching Tips 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. C++ Programming: Program Design Including Data Structures, Fourth Edition 6-3 Quick Quiz 1 1. How many parameters does the C++ predefined function pow have? Answer: two 2. The C++ function sqrt is of type ____________________. Answer: double 3. To use predefined functions in a program, you must include the ____________________ that includes the function specification. Answer: header file 4. The C++ function toupper returns a(n) ____________________ value. Answer: int User-Defined Functions Teaching Tip Before introducing user-defined functions, 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 development process by isolating code, and allow several people to work on different portions of a program. 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. 1. Describe the two categories of user-defined functions in C++: value-returning functions and void functions. Value-Returning Functions 1. Briefly describe the items a programmer needs to know when using a predefined function, including the header file name, the function name, the parameter type, and the return type. 2. Discuss how the return value of a function is used in a program. C++ Programming: Program Design Including Data Structures, Fourth Edition Teaching Tip 6-4 Use code snippets from the text to demonstrate how a function call can be used in both an assignment statement and an output statement. 3. Define the terms function header, function body, and function definition. Use code examples to describe the elements of a function. 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. 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 a return type and identifier as 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. C++ Programming: Program Design Including Data Structures, Fourth Edition 6-5 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 Briefly relate the actual parameter list syntax to the function call syntax to make sure students understand how to write a complete function call expression. Also, compare an actual function call with the corresponding function header to demonstrate the one-to-one correspondence and matching data types of the parameters. Quick Quiz 2 1. A value-returning function is used (called) in a(n) ____________________. Answer: expression 2. True or False: Void functions do not have a return type. Answer: True 3. The code required to accomplish a function’s task is called the ____________________ of the function. Answer: body 4. True or False: An actual parameter is a variable declared in the function heading. Answer: False 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. 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. C++ Programming: Program Design Including Data Structures, Fourth Edition 6-6 Function Prototype 1. Define the term function prototype and explain how it is used in C++ programs to avoid compilation errors. Syntax: Function Prototype 1. Step through the syntax of a function prototype. Emphasize the syntactic differences between a function prototype and a function header. 2. Discuss the examples in the “Notes” at the end of this section to reiterate that a valuereturning function must return a value, and the data type of the value must match the function type. Palindrome Number (Example 6-4) 1. Discuss the Palindrome program and the function isNumPalindrome in particular. Note that this function returns a bool value. Flow of Execution 1. Describe in detail the flow of execution in a program with functions. Review the use of function prototypes to facilitate compilation. Also, explain how a function call operates during program execution. 2. Step through the “Largest Number” and “Cable Company” Programming Examples to consolidate the concepts presented in this chapter. Encourage students to compile and run these programs on their own. Quick Quiz 3 1. What is a local declaration? Answer: A declaration of a variable inside a function for use only within that function 2. True or False: When a return statement executes in a function, the function immediately terminates and the control goes back to the caller. Answer: True 3. True or False: A return statement must be the last statement in a function. Answer: False 4. A(n) ____________________ is a function heading without the body of the function. Answer: function prototype C++ Programming: Program Design Including Data Structures, Fourth Edition 6-7 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? You may wish to use the functions on page 316 to facilitate the discussion. 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 of the predefined functions in the C++ library (such as log), or another type of arithmetic function, such as the factorial function. You may reuse functions if you have already implemented them as part of a previous programming project. 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 research to determine how to use this function to retrieve the current time and display it to the standard output device. Test your functions by writing the appropriate function calls in the main function. Additional Resources 1. C++ Notes: Functions: www.fredosaurus.com/notes-cpp/90summaries/30funcsum.html 2. cmath Library: www.cplusplus.com/ref/cmath/ 3. ctime(): www.cprogramming.com/fod/ctime.html C++ Programming: Program Design Including Data Structures, Fourth Edition 6-8 Key Terms Actual parameter: variable or expression listed in a call to a function Body (of a function): code required to accomplish the task Data type (of a function): return type of a value-returning function Definition (of a function): includes the name of the function, a listing of the parameters if any, the data type of each parameter, the data type of the value returned by the function, and the code required to accomplish the task Formal parameter: a variable declared in the function heading Function header (heading): includes the name of the function, the number of parameters if any, the data type of each parameter, and the data type of the value returned by the function Function prototype: function heading without the body of the function Return type (of a function): type of the value that the function returns; also called the data type of a function Local declaration: declaration of a variable within a block of code for use only within that block Module: another name for a function Parameter: may be formal or actual Value-returning functions: functions that have a return type Void functions: functions that do not have a data type