Download presentation
Presentation is loading. Please wait.
Published byCory Scott Modified over 8 years ago
1
An Introduction to Programming with C++1 Void Functions Tutorial 5
2
An Introduction to Programming with C++2 Objectives Create and invoke a function that does not return a value Pass information, by reference, to a function Pass a String variable, by value and by reference, to a function
3
An Introduction to Programming with C++3 Concept Lesson
4
An Introduction to Programming with C++4 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
5
An Introduction to Programming with C++5 More About Functions
6
An Introduction to Programming with C++6 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
7
An Introduction to Programming with C++7 Creating Void Functions
8
An Introduction to Programming with C++8 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 Creating Void Functions
9
An Introduction to Programming with C++9 Passing Variables Most programming languages allow you to pass to the receiving function either: –the variable’s value (pass by value) –or its address (pass by reference)
10
An Introduction to Programming with C++10 Passing Variables by Value
11
An Introduction to Programming with C++11 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 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
12
An Introduction to Programming with C++12 Passing Variables by Reference
13
An Introduction to Programming with C++13 Passing Variables by Value and by Reference
14
An Introduction to Programming with C++14 Passing Variables by Value and by Reference
15
An Introduction to Programming with C++15 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 Passing Variables by Value and by Reference
16
An Introduction to Programming with C++16 Passing Variables by Value and by Reference
17
An Introduction to Programming with C++17 Passing Variables by Value and by Reference
18
An Introduction to Programming with C++18 Passing Variables by Value and by Reference
19
An Introduction to Programming with C++19 Passing a String Variable to a Function String data type is not a fundamental data type in C++. –It is a data type that is added to the C++ language through the use of a class To pass a String variable by value to a C++ function: –use the syntax String * variablename in the function header, and the syntax String * in the function prototype To pass a String variable by reference to a C++ function: –use the syntax String *& variablename in the function header, and the syntax String *& in the function prototype
20
An Introduction to Programming with C++20 Passing a String Variable by value and by reference
21
An Introduction to Programming with C++21 Application Lesson
22
An Introduction to Programming with C++22 Analyzing, Planning, and Desk- checking
23
An Introduction to Programming with C++23 Data for first desk-check –Customer name: Joe Smith –Current reading (gallons): 9000 –Previous reading (gallons): 8000 –Rate per gallon:.00175 Data for second desk-check –Customer name: Suman Patel –Current reading (gallons): 14000 –Previous reading (gallons): 7500 –Rate per gallon:.00175 Analyzing, Planning, and Desk-checking
24
An Introduction to Programming with C++24 Completed Desk-check Tables
25
An Introduction to Programming with C++25 Coding the main() Function
26
An Introduction to Programming with C++26 Coding the getInput() Function The main() function will pass to the getInput() function the memory addresses of its name, current, and previous variables The getInput() function will use the following formal parameters to receive the information: –&cust –&cur –&prev
27
An Introduction to Programming with C++27 Code for the getInput() Function
28
An Introduction to Programming with C++28 Coding the calculate() Function The main() function will pass to the calculate() function: –the values of three memory locations (current, previous, and RATE) –the addresses of two memory locations (gallons and charge)
29
An Introduction to Programming with C++29 Code for the calculate() Function
30
An Introduction to Programming with C++30 Coding the displayBill() Function
31
An Introduction to Programming with C++31 Completing the Water Bill Program To open the partially completed C++ program: 1.Start Microsoft Visual Studio.NET. If necessary, close the Start Page window 2.Click File on the menu bar, and then click Open Solution. The Open Solution dialog box opens 3.Locate and then open the CppNet\Tut05\T5App Solution folder 4.Click T5App Solution (T5App Solution.sln) in the list of filenames, and then click the Open button 5.If the T5App.cpp source file is not displayed, right-click T5App.cpp in the Solution Explorer window, and then click Open
32
An Introduction to Programming with C++32 Testing the Program To test the program: 1.Save and then build the solution. Verify that the program generated no warnings 2.Execute the program. When prompted for the customer’s name, type Joe Brown and press Enter 3.When prompted for the current reading, type 9000 and press Enter. When prompted for the previous reading, type 8000 and press Enter
33
An Introduction to Programming with C++33 Command Prompt Window Showing the Results of the First Test
34
An Introduction to Programming with C++34 Testing the Program (Cont.) 4.Press Enter to close the Command Prompt window, then close the Output window 5.Execute the program again. Enter Suman Patel as the customer name, 14000 as the current reading, and 7500 as the previous reading. The Command Prompt window shows that the gallons used and the water charge are 6,500 and $11.38, respectively, which agree with the results shown earlier in the desk-check tables. 6.Press Enter to close the Command Prompt window, then close the Output window 7.Click File on the menu bar, and then click Close Solution to close the current solution 8.Click File on the menu bar, and then click Exit to exit Visual Studio.NET
35
An Introduction to Programming with C++35 Summary Create and invoke void functions Pass information by reference to a function –Allows function to change variable that is passed in Pass a String variable by value and by reference to a function
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.