Chapter 10 Void Functions Introduction to Programming with


















- Slides: 18

Chapter 10: Void Functions Introduction to Programming with C++ Fourth Edition 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 2

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 3

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 4

Creating Void Functions (continued) Introduction to Programming with C++, Fourth Edition 5

Creating Void Functions (continued) • Differences between the syntax of a valuereturning 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 6

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 7

Passing Variables by Value Introduction to Programming with C++, Fourth Edition 8

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 9

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 10

Passing Variables by Reference (continued) Introduction to Programming with C++, Fourth Edition 11

Passing Variables by Value and by Reference Introduction to Programming with C++, Fourth Edition 12

Passing Variables by Value and by Reference (continued) Introduction to Programming with C++, Fourth Edition 13

Passing Variables by Value and by Reference (continued) • salary and raise. Rate: 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 new. Salary: 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 14

Passing Variables by Value and by Reference (continued) Introduction to Programming with C++, Fourth Edition 15

Passing Variables by Value and by Reference (continued) Introduction to Programming with C++, Fourth Edition 16

Passing Variables by Value and by Reference (continued) Introduction to Programming with C++, Fourth Edition 17

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 18