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

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

  • View
    237

  • Download
    0

Embed Size (px)

Citation preview

Page 1: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++

Fifth Edition

Chapter 10Void Functions

Page 2: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 2

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

• Use void functions in a .NET C++ program

Page 3: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 3

Concept Lesson

• More About Functions

• Creating Void Functions

• Passing Variables to a Function

• Passing Variables By Value and By Reference

Page 4: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 4

More About Functions

• Use functions to avoid duplicating code– They also allow large and complex programs to be

broken into small and manageable tasks

• main() is typically responsible for invoking each of the other functions when needed– Program-defined functions streamline main()

• Functions can be:– Value-returning– Void

Page 5: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 5

Creating Void Functions

• Void functions do not return a value after completing their assigned tasks– E.g., to display information (such as a title and

column headings) at the top of each page in a report• Avoids repeating code several times in program

Page 6: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 6

Creating Void Functions (continued)

Page 7: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 7

Passing Variables to a Function

• A variable has both a value and a unique address (memory location)

• You can pass either the variable’s value or its address to the receiving function– Pass by value– Pass by reference

Page 8: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 8

Passing Variables By Value

• In a pass by value, the computer passes the contents of the variable to the receiving function– Receiving function is not given access to the variable

in memory• It cannot change value stored inside variable

• By default, variables are passed by value in C++

Page 9: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 9

Passing Variables By Value (continued)

Page 10: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 10

Passing Variables By Value (continued)

Page 11: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 11

Passing Variables By Value (continued)

Page 12: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

Passing Variables By Value (continued)

An Introduction to Programming with C++, Fifth Edition 12

Page 13: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 13

Passing Variables By Value (continued)

Page 14: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 14

Passing Variables By Reference

• Passing a variable’s address is referred to as passing by reference– Receiving function has access to passed variable– Use when you want receiving function to change

contents of variable

• To pass a variable by reference in C++– Include an & before the name of the corresponding

formal parameter in receiving function’s header• Called the address-of operator

Page 15: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 15

Passing Variables By Reference (continued)

Page 16: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 16

Passing Variables By Reference (continued)

Page 17: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 17

Passing Variables By Reference (continued)

Page 18: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 18

Passing Variables By Reference (continued)

Page 19: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 19

Passing Variables By Reference (continued)

Page 20: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 20

Passing Variables By Value andBy Reference

• You can mix the way variables are passed when a function is called, passing some by reference and others by value

• Program in Figure 10-15 allows user to enter an employee’s current salary and raise rate– It then calculates and displays the employee’s raise

and new salary amounts

Page 21: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 21

Passing Variables By Value andBy Reference (continued)

Page 22: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 22

Passing Variables By Value andBy Reference (continued)

Page 23: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 23

Passing Variables By Value andBy Reference (continued)

Page 24: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 24

Passing Variables By Value and By Reference (continued)

Page 25: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 25

Summary

• Functions can be:– Value-returning– Void

• Function header begins with void

• Pass by value: value stored inside variable is passed to receiving function

• Pass by reference: variable’s address in memory is passed to receiving function– Receiving function can change variable’s contents– Use & before corresponding formal parameter

Page 26: An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions

An Introduction to Programming with C++, Fifth Edition 26

Application Lesson: Using Void Functions in a C++ Program

• Lab 10.1: Stop and Analyze

• Lab 10.2– Create program to calculate customer’s water bill

• Lab 10.3– Modified program will use two void functions (rather

than one void function) to calculate the number of gallons used and the water charge

• Lab 10.4: Desk-Check Lab

• Lab 10.5: Debugging Lab