An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 11: Classes and Objects
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions.
Chapter 6: User-Defined Functions I
An Introduction to Programming with C++ Fifth Edition
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
Chapter 6: User-Defined Functions I
Chapter 7: Sub and Function Procedures
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CPS120: Introduction to Computer Science Functions.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 8: Advanced Method Concepts. Understanding Parameter Types Mandatory parameter – An argument for it is required in every method call Four types.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
An Introduction to Programming with C++ Sixth Edition
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++1 Void Functions Tutorial 5.
CHAPTER 3 COMPLETING THE PROBLEM- SOLVING PROCESS AND GETTING STARTED WITH C++ An Introduction to Programming with C++ Fifth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Chapter 9: Value-Returning Functions
Chapter 13: Overloading and Templates
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
An Introduction to Programming with C++ Sixth Edition
Completing the Problem-Solving Process
Value-Returning Functions
Chapter 10: Void Functions
The Selection Structure
User-Defined Functions
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Functions Imran Rashid CTO at ManiWeber Technologies.
Chapter 10: Void Functions
Presentation transcript:

An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions

Objectives Create a function that does not return a value Invoke a function that does not return a value Pass information by reference to a function An Introduction to Programming with C++, Sixth Edition2

3 Void Functions Recall that value-returning functions perform a task and then return a single value Void functions also perform tasks but do not return a value A Void function may be used to do something like display information on the screen –Doesn’t need to return a value

An Introduction to Programming with C++, Sixth Edition4 Figure 10-1 How to create a program-defined void function

An Introduction to Programming with C++, Sixth Edition5 Void Functions (cont’d.) Note that header begins with keyword void, instead of a return data type –Indicates that the function does not return a value Function body does not contain a return statement Call a void function by including its name and actual arguments (if any) in a statement Call to a void function appears as a self-contained statement, not part of another statement Execution is same as for value-returning functions

An Introduction to Programming with C++, Sixth Edition6 Figure 10-2 IPO chart information and C++ instructions for the ABC Company program

An Introduction to Programming with C++, Sixth Edition7 Figure 10-2 IPO chart information and C++ instructions for the ABC Company program (cont’d.)

An Introduction to Programming with C++, Sixth Edition8 Figure 10-2 IPO chart information and C++ instructions for the ABC Company program (cont’d.)

An Introduction to Programming with C++, Sixth Edition9 Figure 10-2 IPO chart information and C++ instructions for the ABC Company program (cont’d.)

An Introduction to Programming with C++, Sixth Edition10 Figure 10-2 IPO chart information and C++ instructions for the ABC Company program (cont’d.)

An Introduction to Programming with C++, Sixth Edition11 Figure 10-3 ABC Company program

An Introduction to Programming with C++, Sixth Edition12 Figure 10-3 ABC Company program (cont’d.)

An Introduction to Programming with C++, Sixth Edition13 Figure 10-4 Sample run of the ABC Company program

An Introduction to Programming with C++, Sixth Edition14 Passing Variables to a Function Recall you can pass a variable’s value or its address Passing a variable’s value is referred to as passing by value, while passing a variable’s address is referred to as passing by reference Which one you choose depends on whether the receiving function should have access to the variable in memory Passing by value will not permit the function to change the contents of the variable, but passing by reference will

An Introduction to Programming with C++, Sixth Edition15 Reviewing Passing Variables by Value Passing a variable by value means that only a copy of the variable’s contents is passed, not the address of the variable This means that the receiving function cannot change the contents of the variable It is thus appropriate to pass by value when the receiving function needs to know the value of the variable but does not need to change it

An Introduction to Programming with C++, Sixth Edition16 Figure 10-5 Age message program

An Introduction to Programming with C++, Sixth Edition17 Figure 10-7 Desk-check table after the display Age function header is processed Figure 10-6 Desk-check table after the first three statements in the main function are processed

An Introduction to Programming with C++, Sixth Edition18 Figure 10-9 Sample run of the age message program Figure 10-8 Desk-check table after the display Age function ends

An Introduction to Programming with C++, Sixth Edition19 Passing Variables by Reference Passing a variable’s address in internal memory to a function is referred to as passing by reference You pass by reference when you want the receiving function to change the contents of the variable To pass by reference in C++, you include an ampersand (&) before the name of the formal parameter in the receiving function’s header Ampersand (&) is the address-of operator –Tells the computer to pass the variable’s address rather than a copy of its contents

An Introduction to Programming with C++, Sixth Edition20 Passing Variables by Reference (cont’d.) If receiving function appears below main, you must also include the & in the receiving function’s prototype You enter the & immediately before the name of the formal parameter in the prototype –If the prototype does not contain the formal parameter’s name, you enter a space followed by & after the formal parameter’s data type Void functions use variables passed by reference to send information back to the calling function, instead of a return value

An Introduction to Programming with C++, Sixth Edition21 Figure Modified age message program

An Introduction to Programming with C++, Sixth Edition22 Figure Desk-check table after the get Age function header is processed Figure Desk-check table after the declaration statement in the main function is processed

An Introduction to Programming with C++, Sixth Edition23 Figure Desk-check table after the get Age function ends Figure Desk-check table after the statements in the get Age function are processed

An Introduction to Programming with C++, Sixth Edition24 Figure Desk-check table after the display Age function ends Figure Desk-check table after the computer processes the display Age function header

An Introduction to Programming with C++, Sixth Edition25 Figure Sample run of the modified age message program

An Introduction to Programming with C++, Sixth Edition26 The Salary Program Program that allows the user to enter an employee’s current salary and raise rate Computes the employee’s raise and new salary Program makes use of a void function that is passed two variables by value and two by reference

An Introduction to Programming with C++, Sixth Edition27 The Salary Program (cont’d.) Figure Problem specification, IPO chart information, and C++ instructions for the salary program

An Introduction to Programming with C++, Sixth Edition28 Figure IPO chart information and C++ instructions for the salary program (cont’d.)

An Introduction to Programming with C++, Sixth Edition29 Figure IPO chart information and C++ instructions for the salary program (cont’d.)

An Introduction to Programming with C++, Sixth Edition30 Figure Salary program

An Introduction to Programming with C++, Sixth Edition31 Figure Salary program (cont’d.)

An Introduction to Programming with C++, Sixth Edition32 Figure Sample run of the salary program

An Introduction to Programming with C++, Sixth Edition33 Figure Desk-check table after the computer processes the getNewPayInfo function header Figure Desk-check table after the statements on lines 15 through 24 are processed

An Introduction to Programming with C++, Sixth Edition34 Figure Desk-check table after the getNewPayInfo function ends Figure Desk-check table after the computer processes the statements in the getNewPayInfo function body

Summary All functions are either void or value-returning Value-returning functions return one value Void functions do not return a value Function header of a void function begins with the keyword void instead of a return data type Function body of a void function does not contain a return statement You call a void function by including its name and actual arguments in a statement An Introduction to Programming with C++, Sixth Edition35

Summary (cont’d.) A call to a void function appears as a statement by itself rather than as part of another statement Variables can be passed to functions either by value (the default) or by reference When a variable is passed by value, only a copy of the variable’s value is passed –Receiving function is not given access to the variable, so it cannot change the variable’s contents –Computer uses data type and name of formal parameter to store a copy of the value An Introduction to Programming with C++, Sixth Edition36

Summary (cont’d.) When a variable is passed by reference, the variable’s address in memory is passed –Receiving function can change variable’s contents –Computer assigns name of formal parameter to memory location – variable then has two names To pass by reference you include the address-of operator (&) before the name of the formal parameter in function header If function appears below main, you must also include the & in the function’s prototype An Introduction to Programming with C++, Sixth Edition37

An Introduction to Programming with C++, Sixth Edition38 Lab 10-1: Stop and Analyze Study the code in Figure and then answer the questions (sample run below) Figure Sample run of the program for Lab 10-2

Lab 10-2: Plan and Create An Introduction to Programming with C++, Sixth Edition39 Figure Problem specification and a sample calculation for Lab 10-2

Lab 10-3: Modify Make a copy of Lab 10-2 to modify Current version uses one void function to calculate both the number of units used and the total charge Replace the calcBill functions with two functions: –A void function getUnits that calculates the total number of units used –A value-returning function getTotal that calculates and returns the total charge Test the program appropriately An Introduction to Programming with C++, Sixth Edition40

Lab 10-4: Desk-Check Desk-check the code in Figure using the following four sets of test scores: –78 and 85 –45 and 93 –87 and 98 –54 and 32 An Introduction to Programming with C++, Sixth Edition41

Lab 10-5: Debug Run the program in the Lab10-2.cpp file Enter the following scores: 93, 90, 85, and 100 The program should display 368 as the total points and A as the grade Debug the program An Introduction to Programming with C++, Sixth Edition42