Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions."— Presentation transcript:

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

2 An Introduction to Programming with C++, Fifth Edition2 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

3 An Introduction to Programming with C++, Fifth Edition3 Concept Lesson More About Functions Creating Void Functions Passing Variables to a Function Passing Variables By Value and By Reference

4 An Introduction to Programming with C++, Fifth Edition4 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

5 An Introduction to Programming with C++, Fifth Edition5 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

6 An Introduction to Programming with C++, Fifth Edition6 Creating Void Functions (continued)

7 An Introduction to Programming with C++, Fifth Edition7 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

8 An Introduction to Programming with C++, Fifth Edition8 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++

9 An Introduction to Programming with C++, Fifth Edition9 Passing Variables By Value (continued)

10 An Introduction to Programming with C++, Fifth Edition10 Passing Variables By Value (continued)

11 An Introduction to Programming with C++, Fifth Edition11 Passing Variables By Value (continued)

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

13 An Introduction to Programming with C++, Fifth Edition13 Passing Variables By Value (continued)

14 An Introduction to Programming with C++, Fifth Edition14 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

15 An Introduction to Programming with C++, Fifth Edition15 Passing Variables By Reference (continued)

16 An Introduction to Programming with C++, Fifth Edition16 Passing Variables By Reference (continued)

17 An Introduction to Programming with C++, Fifth Edition17 Passing Variables By Reference (continued)

18 An Introduction to Programming with C++, Fifth Edition18 Passing Variables By Reference (continued)

19 An Introduction to Programming with C++, Fifth Edition19 Passing Variables By Reference (continued)

20 An Introduction to Programming with C++, Fifth Edition20 Passing Variables By Value and By 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

21 An Introduction to Programming with C++, Fifth Edition21 Passing Variables By Value and By Reference (continued)

22 An Introduction to Programming with C++, Fifth Edition22 Passing Variables By Value and By Reference (continued)

23 An Introduction to Programming with C++, Fifth Edition23 Passing Variables By Value and By Reference (continued)

24 An Introduction to Programming with C++, Fifth Edition24 Passing Variables By Value and By Reference (continued)

25 An Introduction to Programming with C++, Fifth Edition25 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

26 An Introduction to Programming with C++, Fifth Edition26 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


Download ppt "An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions."

Similar presentations


Ads by Google