260 likes | 270 Views
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.
E N D
An Introduction to Programming with C++Fifth Edition Chapter 10 Void Functions
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
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
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
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
Creating Void Functions (continued) An Introduction to Programming with C++, Fifth Edition
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
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
Passing Variables By Value (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Value (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Value (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Value (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Value (continued) An Introduction to Programming with C++, Fifth Edition
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
Passing Variables By Reference (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Reference (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Reference (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Reference (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Reference (continued) An Introduction to Programming with C++, Fifth Edition
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
Passing Variables By Value andBy Reference (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Value andBy Reference (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Value andBy Reference (continued) An Introduction to Programming with C++, Fifth Edition
Passing Variables By Value and By Reference (continued) An Introduction to Programming with C++, Fifth Edition
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
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