Presentation is loading. Please wait.

# © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:

## Presentation on theme: "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:"— Presentation transcript:

© 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

© 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” functions. –Create your own functions to perform custom tasks. –Control execution using debugger commands to locate logic errors.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.1Test Driving the Wage Calculator Application

© 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.

© 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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.2C++ Standard Library Functions and Classes (Cont.)

© 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

© 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

© 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 Figure 10.5 Coding the square function. return statement sends a value back to the functions caller

© 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.)

© 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 Arguments: base, exponent 10.3Function Definitions (Cont.)

© 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

© 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

© 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.

© 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

© 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

© 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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10.4 Completing the maximum function (Cont.) Figure 10.13 Maximum application output.

© 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

© 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

© 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.

© 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

© 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

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. WageCalculator.cpp (3 of 4) Prompt for user inputs

© 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

© 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

© 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)

© 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

© 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

© 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

© 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

© 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

© 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

© 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

Download ppt "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:"

Similar presentations

Ads by Google