Chapter 10: Void Functions

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition 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.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
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.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
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.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 3: User-Defined Functions I
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++1 Void Functions Tutorial 5.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 14: Sequential Access Files
Chapter 9: Value-Returning Functions
Programming Logic and Design Seventh Edition
User-Written Functions
More on the Selection Structure
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
Functions and an Introduction to Recursion
Chapter 3: Using Methods, Classes, and Objects
The Selection Structure
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
6.12 Default Arguments.
6 Chapter Functions.
Object-Oriented Programming Using C++ Second Edition
Functions Pass By Value Pass by Reference
Classes and Objects.
Defining Classes and Methods
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Functions and an Introduction to Recursion
Functions Imran Rashid CTO at ManiWeber Technologies.
Chapter 10: Void Functions
Standard Version of Starting Out with C++, 4th Edition
C Parameter Passing.
Presentation transcript:

Chapter 10: Void Functions

Objectives Create a void function Invoke a void function Pass information by reference to a function An Introduction to Programming with C++, Seventh Edition

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++, Seventh Edition

Creating Program-Defined Void Functions Figure 10-2 How to create a program-defined void function An Introduction to Programming with C++, Seventh Edition

Creating Program-Defined 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++, Seventh Edition

Creating Program-Defined Void Functions (cont’d.) Figure 10-4 IPO chart information and C++ instructions for the ABC Company program An Introduction to Programming with C++, Seventh Edition

Creating Program-Defined Void Functions (cont’d.) Figure 10-4 IPO chart information and C++ instructions for the ABC Company program (cont’d.) An Introduction to Programming with C++, Seventh Edition

Creating Program-Defined Void Functions (cont’d.) Figure 10-4 IPO chart information and C++ instructions for the ABC Company program (cont’d.) An Introduction to Programming with C++, Seventh Edition

Creating Program-Defined Void Functions (cont’d.) Figure 10-4 IPO chart information and C++ instructions for the ABC Company program (cont’d.) An Introduction to Programming with C++, Seventh Edition

Creating Program-Defined Void Functions (cont’d.) Figure 10-4 IPO chart information and C++ instructions for the ABC Company program (cont’d.) An Introduction to Programming with C++, Seventh Edition

Creating Program-Defined Void Functions (cont’d.) Figure 10-5 ABC Company program An Introduction to Programming with C++, Seventh Edition

Creating Program-Defined Void Functions (cont’d.) Figure 10-5 ABC Company program (cont’d.) An Introduction to Programming with C++, Seventh Edition

Creating Program-Defined Void Functions (cont’d.) Figure 10-6 Sample run of the ABC Company program An Introduction to Programming with C++, Seventh Edition

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++, Seventh Edition

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++, Seventh Edition

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++, Seventh Edition

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++, Seventh Edition

Passing Variables by Reference (cont’d.) Figure 10-13 Modified age message program An Introduction to Programming with C++, Seventh Edition

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++, Seventh Edition

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++, Seventh Edition

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++, Seventh Edition