Download presentation
Presentation is loading. Please wait.
Published bySimon Scott Modified over 9 years ago
1
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application: Introducing Functions Outline 10.1 Test-Driving the Enhanced Wage Calculator Application 10.2 C++ Standard Library Functions and Classes 10.3 Function Definitions 10.4 Completing the Maximum Application 10.5 Using Functions in the Wage Calculator Application 10.6 Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands 10.7 Wrap-Up
2
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Construct applications modularly from pieces called functions — simplifying your application’s design, implementation and maintenance. –Save time by working with “built-in” library functions. –Create your own programmer-defined functions to perform custom tasks. –Control execution using debugger commands to locate logic errors.
3
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.1Test Driving the Wage Calculator Application
4
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.1Test Driving the Wage Calculator Application (Cont.) Figure 10.1 Wage Calculator output.
5
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.2C++ Standard Library Functions and Classes C++ Standard Library functions and classes –Pre-existing code available for reuse –Designed for maximum efficiency –Tested to eliminate bugs C++ Standard Library Function example –pow (from the library) C++ Standard Library Class example –cout and cin (from the library) Programmer-defined functions –Can be created to meet unique requirements of new applications
6
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. M Y L ITTLE S UBROUTINE D ICTIONARY S UBROUTINE VERSUS F UNCTION Functions and subroutines are the most basic building block you can use to organize your code. Functions are very similar to subroutines; their syntax is nearly identical, and they can both perform the same actions. However, Functions return a value to the code that called it. Subroutines are defined with data type void. The return statement is optional. If you use return to end a Subroutine do not include a return value. In everyday usage, “parameter” and “argument” are used interchangeably to refer to the things that you use to define and call methods or functions. Often this interchangeability doesn’t cause ambiguity. It should be noted, though, that conventionally, they refer to different things.
7
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. M Y L ITTLE S UBROUTINE D ICTIONARY P ARAMETER VERSUS A RGUMENT A “parameter” is the thing used to define a method or function while an “argument” is the thing you use to call a method or function. Parameter: void mySubroutine (unint8_t N){ … } N is a parameter Argument: uint8_t X; X = 10; mySubroutine(X); X is an argument Ultimately, it doesn’t really matter what you say. People will understand from the context. Source: http://www.codeproject.com/KB/aspnet/VBnet_Methods.aspxhttp://www.codeproject.com/KB/aspnet/VBnet_Methods.aspx Source: http://project.ioni.st/post/790http://project.ioni.st/post/790
8
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.2C++ Standard Library Functions and Classes (Cont.)
9
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 10.3 Hypotenuse Calculator template code. 10.3Function Definitions Lengths for sides A and B Error message displays if negative value (or zero) is entered
10
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.3Function Definitions (Cont.) Function header includes: –Return type –Function name –Parameter list Figure 10.4 Defining the square function. Function header A right brace marks the end of a function definition
11
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.3Function Definitions (Cont.) Value returned is the same type declared in the function header To promote reusability, each function should perform a single well defined task Parameter (not argument): side Figure 10.5 Coding the square function. return statement sends a value back to the functions caller
12
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 10.6 Hypotenuse Calculator application updated. 10.3Function Definitions (Cont.)
13
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Calling functions –A function is invoked (or called) by a function call A function call specifies the function name and includes data (arguments) for the callee (function being called) Example Function name: pow Parameters (not arguments): base, exponent 10.3Function Definitions (Cont.)
14
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.3Function Definitions (Cont.) Figure 10.7 Invoking the square function. Calling the square function for each side Arguments (not parameters): sideA, sideB
15
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 10.8 Completing the main function. 10.3Function Definitions (Cont.) Calling cmath function sqrt Formatting and displaying the length of the hypotenuse Can you find the programmer-defined and library functions?
16
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.3Function Definitions (Cont.) Small functions are easier to test, debug and understand than large ones Figure 10.9 Output from the completed Hypotenuse Calculator application.
17
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 10.10 Invoking the determineMaximum function. 10.4 Completing the maximum function Inputting three values Calling the function determineMaximum
18
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.4 Completing the maximum function (Cont.) Function determineMaximum returns the largest of three values Figure 10.11 Defining the determineMaximum function. The empty determineMaximum function
19
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.4 Completing the maximum function (Cont.) Invoking pre-existing function max –Included in the library header file Figure 10.12 max returns the larger of its two arguments. Calling max twice to determine the maximum of three values Returning the maximum of all three values Figure 10.13 Maximum application output.
20
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 10.14 main calls the calculatePay function. 10.5Using Functions in the Wage Calculator Application Inputting wage and hours worked Calling the calculatePay function Formatting and displaying the total wage
21
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 10.15 Defining the calculatePay function. 10.5Using Functions in the Wage Calculator Application Function header Calculating gross wages based on the number of hours worked Return the wages for the week
22
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.5Using Functions in the Wage Calculator Application Figure 10.16 Wage Calculator application output.
23
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. WageCalculator.cpp (1 of 4) The calculatePay function takes two double parameters, returns a double The total variable will contain users wages for the week Nonovertime hour limit
24
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. WageCalculator.cpp (2 of 4) Calculate wages based on the number of hours worked Returning total to calling function Closing brace ends function body
25
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. WageCalculator.cpp (3 of 4) Prompt for user inputs
26
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. WageCalculator.cpp (4 of 4) Call the calculatePay function Format and display value returned from the calculatePay function
27
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.6Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands Step Into command –Executes the next statement in the program If the next statement is a function call, program control goes to that function Step Over command –Acts like the Step Into command If next statement is a function call, the function executes in its entirety and control goes to the next executable line
28
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.6Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.) Step Out command –Executes remaining statements in a function and returns control to the caller Continue Command –Executes until the next breakpoint or the end of main (whichever comes first)
29
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 10.18 Setting a breakpoint in the Wage Calculator. 10.6Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.) Setting a breakpoint
30
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.6Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.) Use the Step Into command to transfer program control to the called function Figure 10.19 Reaching a breakpoint in the Wage Calculator application. Next statement to execute is a function call
31
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.6Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.) Figure 10.20 Stepping into the calculatePay function. Definitions (without assignments) are not considered executable statements Next statement to execute
32
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.6Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.) Figure 10.21 Stepping over a statement in the calculatePay function. Control is transferred to the if…else statement
33
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.6Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.) Figure 10.22 Setting a second breakpoint in the enhanced Wage Calculator application. Setting second breakpoint
34
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.6Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.) Figure 10.23 Using the debugger’s Continue command. Next executable statement
35
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.6Using the Debugger: Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands (Cont.) Figure 10.24 Using the debugger’s Step Over command. The calculatePay function is executed without stepping into it when the Step Over command is selected
36
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 10 − Interest Calculator Application. Turn in annotated source file with your own comments. Answer and Turn-in Tutorial 10 Questions 10.1 to 10.10. Always write the question followed by the answer. Remember to highlight the answer. Exercises 10.11, 10.13, and the Programming Challenge 10.17. For all exercises start with the provided templates. Due next Wednesday
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.