parameter
variable |
reference
parameter |
constant
reference parameter |
new variable,
copying |
reference to an
existing variable |
constant
reference to a value |
input parameter |
may be input and
should be output
parameter |
input parameter |
can modify the
parameter (bad style) |
should modify the parameter | cannot modify the parameter |
can be bound to
any expression |
must be bound to
the variable |
can be bound to any expression |
double future_value(double initial_balance, double p, int n)
{ double r = initial_balance * pow(1 + p / 100, n);
return r;
}
int main()
{ cout << "Please enter the interest rate in percent: ";
double r;
cin >> r;
double balance = future_value(10000, r, 10);
cout << "After 10 years the balance is" << balance
<< "\n";
return 0;
}
/**
Turns a number into its English name.
@param n a positive integer < 1 000 000 (e.g. 274)
@return the name of n (e.g. "two hundred seventy four")
*/
string int_name(int n);
/**
Turns a digit into its English name
@param n an integer between 1 and 9
@return the name of n ("one" ... "nine")
*/
string digit_name(int n);
/**
Turns a number between 10 and 19 into its English name.
@param n an integer between 10 and 19
@return the name of n ("ten"..."nineteen")
*/
string teen_name(int n);
/**
Gives the English name of a multiple of 10
@param n an integer between 2 and 9
@return the name of 10 * n ("twenty"..."ninety")
*/
string tens_name(int n);
string int_name(int n)
{ int c = n; /* the part that needs to be converted */
string r; /* the return value */
if (c >= 1000)
{ r = name of thousands in c + "thousand"
remove thousands from c
}
if (c >= 100)
{ r = r + name of hundreds in c + "hundreds"
remove hundreds from c
}
if (c >= 20)
{ r = r + name of tens in c
remove tens from c
}
if (c >= 10) // && c < 20
{ r = r + name of c
c = 0
}
if (c > 0)
r = r + name of c;
return r;
}
if (c >= 1000)
{ r = int_name(c / 1000) + " thousand";
c = c % 1000;
}
int_name(416) c r 416 ""
int_name(416) c r 416 ""
digit_name(4) Returns "four"
int_name(416) c r416"" 16 "four hundred"
int_name(416) c r416""16"four hundred" 0 "four hundred sixteen"
Syntax 5.6: Assertions assert(expression);
|
double future_value(double initial_balance, double p, int n)
{ assert(p >= 0);
assert(n >= 0);
return initial_balance * pow(1 + p / 100, n);
}
/**
Computes the value of an investment with compound interest.
@param initial_balance the initial value of the investment
@param p the interest rate in percent; must be >= 0
@param n the number of periods the investment is held; must be >= 0
@return the balance after n periods
*/