1 / 26

An Introduction to Programming with C++ Fifth Edition

Learn how to create and invoke void functions, pass information by reference, and use void functions in .NET C++ programs. Avoid repeating code and streamline your programs by breaking them into manageable tasks.

cdudley
Download Presentation

An Introduction to Programming with C++ Fifth Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  6. Creating Void Functions (continued) 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 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++ An Introduction to Programming with C++, Fifth Edition

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

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

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

  12. Passing Variables By Value (continued) An Introduction to Programming with C++, Fifth Edition

  13. Passing Variables By Value (continued) 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 An Introduction to Programming with C++, Fifth Edition

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

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

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

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

  19. Passing Variables By Reference (continued) 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 An Introduction to Programming with C++, Fifth Edition

  21. Passing Variables By Value andBy Reference (continued) An Introduction to Programming with C++, Fifth Edition

  22. Passing Variables By Value andBy Reference (continued) An Introduction to Programming with C++, Fifth Edition

  23. Passing Variables By Value andBy Reference (continued) An Introduction to Programming with C++, Fifth Edition

  24. Passing Variables By Value and By Reference (continued) 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 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 An Introduction to Programming with C++, Fifth Edition

More Related