Ch05_Functions for All Subtasks
Ch05_Functions for All Subtasks
Object-Oriented Programming
Chapter 5
Functions for All Subtasks
康立威
國立台灣師範大學電機工程系
[email protected]
分機: 3563
◼ One value
◼ Example:
showResults(32.5, 0.3);
parameters
◼ It is fairly common to have no parameters in
void-functions
◼ In this case there will be no arguments in the function call
◼ Statements in function body are executed
◼ Optional return statement ends the function
◼ Return statement does not include a value to return
◼ Return statement is implicit if it is not included
C = (5/9) (F – 32)
Display 5.3
Display 5.4
age 1001 34
initial 1002 A
hours 1003 23.5
1004
◼ Example:
void goodStuff(int& par1, int par2, double& par3);
◼ par1 and par3 are call-by-reference formal parameters
Display 5.6
Display 5.7
call-by-reference parameter?
◼ Write a function with both call-by-value and
call-by-reference parameters
int main()
{
double wholesaleCost, retailPrice;
int shelftime;
getInput(wholesaleCost, shelftime);
retailPrice = price(wholesaleCost, shelftime);
giveOutput(wholesaleCost, shelftime, retailPrice);
return 0;
}
//HIGH_MARKUP
◼ 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
◼ In function price, 7 days is a boundary condition
◼ Test for exactly 7 days as well as one day more and one day
less
◼ Can you
◼ Define a function in the body of another
function?
◼ Write a stub?
◼ = instead of ==
#include <cassert>
assert(boolean expression)
◼ If the boolean is false then the program will abort