0% found this document useful (0 votes)
25 views

Chapter 10 Void Functions

Uploaded by

Ian Vital
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Chapter 10 Void Functions

Uploaded by

Ian Vital
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Introduction to Programming in C++

Eighth Edition

Chapter 10: Void Functions

© 2016 Cengage Learning®. May not be scanned, copied or


duplicated, or posted to a publicly accessible website, in whole
or in part.
Objectives

• Create a void function


• Invoke a void function
• Pass information by reference to a function

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 2
or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 3
or in part.
Functions (cont’d.)

Figure 10-1 Illustration of value-returning and void functions

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 4
or in part.
Creating Program-Defined Void
Functions

Figure 10-2 How to create a program-defined void function


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 5
or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 6
or in part.
Creating Program-Defined Void
Functions (cont’d.)

Figure 10-3 How to call (invoke) a void function

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 7
or in part.
Creating Program-Defined Void
Functions (cont’d.)

Figure 10-4 Problem specification, IPO chart information and


C++ instructions for the Martin Sports Program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 8
or in part.
Creating Program-Defined Void
Functions (cont’d.)

Figure 10-4 Problem specification, IPO chart information and


C++ instructions for the Martin Sports Program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 9
or in part.
Creating Program-Defined Void
Functions (cont’d.)

Figure 10-4 Problem specification, IPO chart information and


C++ instructions for the Martin Sports Program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 10
or in part.
Creating Program-Defined Void
Functions (cont’d.)

Figure 10-5 Martin Sports Program


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 11
or in part.
Creating Program-Defined Void
Functions (cont’d.)

Figure 10-6 Sample run of the Martin Sports Program

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 12
or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 13
or in part.
Passing Variables to a Function
(cont’d.)

Figure 10-7 Illustrations of passing by value and passing by reference

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 14
or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 15
or in part.
Reviewing Passing Variables by Value
(cont’d.)

Figure 10-8 Beginning of Company Ratings Program


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 16
or in part.
Reviewing Passing Variables by Value
(cont’d.)

Figure 10-8 Completion of Company Ratings Program and sample run


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 17
or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 18
or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 19
or in part.
Passing Variables by Reference
(cont’d.)

Figure 10-11 Beginning of Tips Program


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 20
or in part.
Passing Variables by Reference
(cont’d.)

Figure 10-11 Completion of Tips Program and sample run


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 21
or in part.
Passing Variables by Reference
(cont’d.)

Figure 10-12 Desk-check table before the getTips function is called

Figure 10-13 Desk-check table after getTips function header is processed

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 22
or in part.
Passing Variables by Reference
(cont’d.)

Figure 10-14 Desk-check table after the statements in the getTips


function are processed

Figure 10-15 Desk-check table after getTips function ends

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 23
or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 24
or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 25
or in part.
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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 26
or in part.
Lab 10-1: Stop and Analyze

• Study the code in Figure 10-17 and then answer the


questions (sample run below)

Figure 10-16 Sample run of program for Lab 10-1


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 27
or in part.
Lab 10-1: Stop and Analyze (cont’d.)

Figure 10-17 Beginning of code for Lab 10-1


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 28
or in part.
Lab 10-1: Stop and Analyze (cont’d.)

Figure 10-17 Completion of code for Lab 10-1


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 29
or in part.
Lab 10-2: Plan and Create

Figure 10-18 Problem specification and a sample calculation for Lab 10-2

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 30
or in part.
Lab 10-2: Plan and Create (cont’d.)

Figure 10-21 Beginning of IPO chart information


and C++ instructions for Lab 10-2’s program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 31
or in part.
Lab 10-2: Plan and Create (cont’d.)

Figure 10-21 Completion of IPO chart information


and C++ instructions for Lab 10-2’s program
© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 32
or in part.
Lab 10-2: Plan and Create (cont’d.)

Figure 10-22 Beginning of Lab 10-2’s program


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 33
or in part.
Lab 10-2: Plan and Create (cont’d.)

Figure 10-22 Completion of Lab 10-2’s program

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 34
or in part.
Lab 10-3: Modify

• Modify the program in Lab10-2.cpp by changing the


getRegular and getBoGo functions to value-returning
functions. Save the modified program as Lab10-3.cpp
• Save, run, and test the program.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 35
or in part.
Lab 10-4: What’s Missing?

• The program is this lab should display an employee’s


raise and new salary, given his or her current salary.
• Follow the instructions for starting C++ and opening the
Lab10-4.cpp file. Put the C++ instructions in the proper
order, and then determine the one or more missing
instructions.
• Test the program appropriately.

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 36
or in part.
Lab 10-5: Desk-Check

• Desk-check the code in Figure 10-24. What will the


program display?

Figure 10-24 Beginning of code for Lab 10-5


© 2016 Cengage Learning®. May not be scanned, copied or
An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 37
or in part.
Lab 10-5: Desk-Check

Figure 10-24 Completion of code for Lab 10-5

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 38
or in part.
Lab 10-6: Debug

• Run the program in the Lab10-6.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

© 2016 Cengage Learning®. May not be scanned, copied or


An Introduction to Programming with C++, Eighth Edition duplicated, or posted to a publicly accessible website, in whole 39
or in part.

You might also like