Professional Documents
Culture Documents
Overview
5.1 void Functions
5.2 Call-By-Reference Parameters
5.3 Using Procedural Abstraction
5.4 Testing and Debugging
5.5 General Debugging Techniques
Slide 5- 3
5.1
void Functions
void-Functions
Slide 5- 5
void-Function Definition
Display 5.1
Slide 5- 6
Using a void-Function
Slide 5- 7
void-Function Calls
Slide 5- 8
Example:
Converting Temperatures
Slide 5- 9
void-Functions
Why Use a Return?
Display 5.3
Copyright 2012 Pearson Addison-Wesley. All rights reserved.
Slide 5- 10
Slide 5- 11
Can you
Describe the differences between voidfunctions and functions that return one value?
Tell what happens if you forget the returnstatementin a void-function?
Distinguish between functions that are used as
expressions and those used as statements?
Slide 5- 12
5.2
Call-By-Reference Parameters
Call-by-Reference Parameters
Slide 5- 14
Call-by-Reference Example
Slide 5- 15
Call-By-Reference Details
Slide 5- 16
Call Comparisons
Call By Reference vs Value
Call-by-reference
The function call:
f(age);
Call-by-value
The function call:
f(age);
Memory
Name
Location
Contents
age
1001
34
initial
1002
hours
1003
23.5
1004
Example: swap_values
Slide 5- 18
Example:
void good_stuff(int& par1, int par2, double& par3);
par1 and par3 are call-by-reference formal parameters
Slide 5- 19
Display 5.6
Slide 5- 20
Display 5.7
Slide 5- 21
Can you
Write a void-function definition for a function called
zero_both that has two reference parameters, both
of which are variables of type int, and sets the values
of both variables to 0.
Write a function that returns a value and has a
call-by-reference parameter?
Write a function with both call-by-value and
call-by-reference parameters
Slide 5- 22
5.3
Using Procedural Abstraction
Slide 5- 24
Precondition
States what is assumed to be true when the function
is called
Postcondition
Describes the effect of the function call
Tells what will be true after the function is executed
(when the precondition holds)
If the function returns a value, that value is described
Changes to call-by-reference parameters are
described
Slide 5- 26
swap_values revisited
Slide 5- 27
Function celsius
Slide 5- 28
Slide 5- 29
Case Study
Supermarket Pricing
Problem definition
Determine retail price of an item given suitable input
5% markup if item should sell in a week
10% markup if item expected to take more than a
week
Input
Output
Slide 5- 30
Supermarket Pricing:
Problem Analysis
Slide 5- 31
Supermarket Pricing:
Function get_input
Slide 5- 32
Supermarket Pricing:
Function price
Slide 5- 33
Supermarket Pricing:
Function give_output
Slide 5- 34
Supermarket Pricing:
The main function
get_input(wholesale_cost, shelf_time);
retail_price = price(wholesale_cost, shelf_time);
give_output(wholesale_cost, shelf_time, retail_price);
return 0;
Slide 5- 35
Supermarket Pricing:
Algorithm Design -- price
Slide 5- 36
Supermarket Pricing:
Constants for The price Function
Slide 5- 37
Supermarket Pricing:
Coding The price Function
Slide 5- 38
Supermarket Pricing :
Program Testing
Testing strategies
Use data that tests both the high and low markup
cases
Test boundary conditions, where the program is
expected
to change behavior or make a choice
Slide 5- 39
Can you
Define a function in the body of another
function?
Slide 5- 40
5.4
Testing and Debugging
Slide 5- 42
Stubs
Slide 5- 43
Slide 5- 44
Can you
Describe the fundamental rule for testing functions?
Write a stub?
Slide 5- 45
5.5
General Debugging
Techniques
Slide 5- 47
Slide 1- 48
Use a debugger
Tool typically integrated with a development
environment that allows you to stop and step through a
program line-by-line while inspecting variables
The assert macro
Can be used to test pre or post conditions
#include <cassert>
assert(boolean expression)
If the boolean is false then the program will abort
Slide 5- 49
Assert Example
Slide 5- 50
Can you
Recognize common errors?
Slide 5- 51
Chapter 5 -- End
Slide 5- 52
Display 5.1
Back
Next
Slide 5- 53
Next
Slide 5- 54
Display 5.2
(2/2)
Back
Next
Slide 5- 55
Display 5.3
Back
Next
Slide 5- 56
Next
Slide 5- 57
Display 5.4
(2/2)
Back
Next
Slide 5- 58
Display 5.5
(1/2)
Back
Next
Slide 5- 59
Display 5.5
(2/2)
Back
Next
Slide 5- 60
Display 5.6
Back
Next
Slide 5- 61
Display 5.7
Back
Next
Slide 5- 62
Next
Slide 5- 63
Display 5.8
(2/2)
Back
Next
Slide 5- 64
Next
Slide 5- 65
Next
Slide 5- 66
Display 5.9
(3/3)
Back
Next
Slide 5- 67
Next
Slide 5- 68
Display 5.10
(2/2)
Back
Next
Slide 5- 69
Next
Slide 5- 70
Next
Slide 5- 71
Display 5.12
Back
Next
Slide 5- 72
Display 5.13 (1 of 2)
Back
Next
Slide 5- 73
Display 5.13 (2 of 2)
Back
Next
Slide 5- 74