Chapter 10: Void Functions Introduction to Programming with C++ Fourth Edition
Objectives Create and invoke a function that does not return a value Pass information, by reference, to a function Introduction to Programming with C++, Fourth Edition
More About Functions Programmers use functions for two reasons: To avoid duplicating code in different parts of a program To allow large and complex programs to be broken into small and manageable tasks Introduction to Programming with C++, Fourth Edition
Creating Void Functions Void function - a function that does not return a value after completing its assigned task You might want to use a void function in a program simply to display information: Example: title and column headings at the top of each page in a report Introduction to Programming with C++, Fourth Edition
Creating Void Functions (continued) Introduction to Programming with C++, Fourth Edition
Creating Void Functions (continued) Differences between the syntax of a value-returning function and that of a void function: The function header in a void function begins with the keyword void, rather than with a data type The keyword void indicates that the function does not return a value The function body in a void function does not contain a return expression; statement Introduction to Programming with C++, Fourth Edition
Passing Variables Most programming languages allow you to pass to the receiving function either: The variable’s value (pass by value) Its address (pass by reference) Introduction to Programming with C++, Fourth Edition
Passing Variables by Value Introduction to Programming with C++, Fourth Edition
Passing Variables by Reference Passing a variable’s address (passing by reference) gives the receiving function access to the variable being passed You pass a variable by reference when you want the receiving function to change the contents of the variable Introduction to Programming with C++, Fourth Edition
Passing Variables by Reference (continued) To pass a variable by reference to a C++ function: Include an ampersand (&), called the address-of operator, before the name of the corresponding formal parameter in the function header Introduction to Programming with C++, Fourth Edition
Passing Variables by Reference (continued) Introduction to Programming with C++, Fourth Edition
Passing Variables by Value and by Reference Introduction to Programming with C++, Fourth Edition
Passing Variables by Value and by Reference (continued) Introduction to Programming with C++, Fourth Edition
Passing Variables by Value and by Reference (continued) salary and raiseRate: passed by value because the receiving function needs to know the values stored in the variables, but does not need to change the values raise and newSalary: passed by reference, because it is the receiving function’s responsibility to calculate the raise and new salary amounts and then store the results in these memory locations Introduction to Programming with C++, Fourth Edition
Passing Variables by Value and by Reference (continued) Introduction to Programming with C++, Fourth Edition
Passing Variables by Value and by Reference (continued) Introduction to Programming with C++, Fourth Edition
Passing Variables by Value and by Reference (continued) Introduction to Programming with C++, Fourth Edition
Summary Void function - a function that does not return a value after completing its assigned task Pass information by reference to a function Function can change the variable that is passed in Introduction to Programming with C++, Fourth Edition