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

Slides:



Advertisements
Similar presentations
4 Control Statements: Part 1.
Advertisements

Chapter 6: User-Defined Functions I
Chapter 5: Control Structures II (Repetition)
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 16 – Shopping List Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
Squares and Square Root WALK. Solve each problem REVIEW:
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Chapter 6: User-Defined Functions I
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhancing the Wage Calculator Application Introducing Function Procedures and.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3Math Library Functions Math library functions –perform.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
13 Enhancing the Wage Calculator App
Dr. Shady Yehia Elmashad
User-Defined Functions
Chapter 6: User-Defined Functions I
Functions Imran Rashid CTO at ManiWeber Technologies.
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 Invoking the determineMaximum function 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 Completing the maximum function (Cont.) Function determineMaximum returns the largest of three values Figure Defining the determineMaximum function. The empty determineMaximum function

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Completing the maximum function (Cont.) Invoking pre-existing function max –Included in the library header file Figure 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 Completing the maximum function (Cont.) Figure Maximum application output.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 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 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 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 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 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 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 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 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 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 Using the debugger’s Step Over command. The calculatePay function is executed without stepping into it when the Step Over command is selected