Void Functions - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Void Functions

Description:

Pass information, by reference, to a function ... Include an ampersand (&), called the address-of operator, before the name of the ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 36
Provided by: www2Pie
Category:

less

Transcript and Presenter's Notes

Title: Void Functions


1
Void Functions
  • Tutorial 5

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
Concept Lesson
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
More About Functions
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
Creating Void Functions
8
Creating 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

9
Passing 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)

10
Passing Variables by Value
11
Passing 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

12
Passing Variables by Reference
13
Passing Variables by Value and by Reference
14
Passing Variables by Value and by Reference
15
Passing 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

16
Passing Variables by Value and by Reference
17
Passing Variables by Value and by Reference
18
Passing Variables by Value and by Reference
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
Passing a String Variable by value and by
reference
21
Application Lesson
22
Analyzing, Planning, and Desk-checking
23
Analyzing, 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
25
Coding the main() Function
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
Code for the getInput() Function
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
Code for the calculate() Function
30
Coding the displayBill() Function
31
Completing 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

32
Testing 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

33
Command Prompt Window Showing the Results of the
First Test
34
Testing 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

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
Write a Comment
User Comments (0)
About PowerShow.com