Title: Void Functions
1Void Functions
2Objectives
- 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
3Concept Lesson
4More 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
5More About Functions
6Creating 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
7Creating Void Functions
8Creating Void Functions
- 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
9Passing Variables
- Most programming languages allow you to pass to
the receiving function either - the variables value (pass by value)
- or its address (pass by reference)
10Passing Variables by Value
11Passing Variables by Reference
- Passing a variables 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
12Passing Variables by Reference
13Passing Variables by Value and by Reference
14Passing Variables by Value and by Reference
15Passing Variables by Value and by Reference
- 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 functions responsibility to
calculate the raise and new salary amounts and
then store the results in these memory locations
16Passing Variables by Value and by Reference
17Passing Variables by Value and by Reference
18Passing Variables by Value and by Reference
19Passing 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
20Passing a String Variable by value and by
reference
21Application Lesson
22Analyzing, Planning, and Desk-checking
23Analyzing, Planning, and Desk-checking
- 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
24 Completed Desk-check Tables
25Coding the main() Function
26Coding 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
27Code for the getInput() Function
28Coding 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)
29Code for the calculate() Function
30Coding the displayBill() Function
31Completing the Water Bill Program
- To open the partially completed C program
- Start Microsoft Visual Studio .NET. If necessary,
close the Start Page window - Click File on the menu bar, and then click Open
Solution. The Open Solution dialog box opens - Locate and then open the CppNet\Tut05\T5App
Solution folder - Click T5App Solution (T5App Solution.sln) in the
list of filenames, and then click the Open button - If the T5App.cpp source file is not displayed,
right-click T5App.cpp in the Solution Explorer
window, and then click Open
32Testing the Program
- To test the program
- Save and then build the solution. Verify that the
program generated no warnings - Execute the program. When prompted for the
customers name, type Joe Brown and press Enter - When prompted for the current reading, type 9000
and press Enter. When prompted for the previous
reading, type 8000 and press Enter
33Command Prompt Window Showing the Results of the
First Test
34Testing the Program (Cont.)
- Press Enter to close the Command Prompt window,
then close the Output window - 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. - Press Enter to close the Command Prompt window,
then close the Output window - Click File on the menu bar, and then click Close
Solution to close the current solution - Click File on the menu bar, and then click Exit
to exit Visual Studio .NET
35Summary
- 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