int main()
{ cout << "Enter a positive number: ";
double x;
cin >> x;
x = sqrt(x); // sqrt is running
cout << "The result is " << x << "\n";
return 0;
}
|
|
member-function | implicit parameter | implicit parameter type |
harry.get_salary() | harry | Employee |
t.add_seconds(10) | t | Time |
msg.move(dx,dy) | msg | Message |
function | return value |
sqrt(x) | double type |
c.get_center() | Point type |
m.get_text() | string type |
getline(cin,s) | no return value |
function |
requirements for parameters |
fabs(w) | the parameter w must be double type, constant or variable |
t.second_from(t1) | the parameter t1 must be an object from Time class, constant or variable |
getline(cin,s) | the first parameter must be cin, the second parameter must be a variable of the type string |
double future_value(double p)
double future_value(double p)
{
. . .
}
double future_value(double p)
{ double b = 1000 * pow(1 + p / 100, 10);
return b;
}
Syntax 5.1: Function Definition return_type function_name(parameter1, ..., parametern)
|
double future_value(double initial_balance, double p, int n)
{ double b = initial_balance * pow(1 + p / 100, n);
return b;
}
double balance = future_value(1000, rate, 10);
/**
Computes the value of an investment with compound interest.
@param initial_balance - the initial value of the investment
@param p the interest rate per period in percent
@param n the number of periods the investment is held
@return the balance after n periods
*/
double future_value(double initial_balance, double p, int n)
{ double b = initial_balance * pow(1 + p / 100, n);
return b;
}
double future_value(double initial_balance, double p, int n)
{ if (n < 0) return 0;
if (p < 0) return 0;
double b = initial_balance * pow(1 + p / 100, n);
return b;
}
double future_value(double initial_balance, double p, int n)
{ if (p >= 0)
return initial_balance * pow(1 + p / 100, n);
/* Error */
}
bool approx_equal(double x, double y)
{ const double EPSILON = 1E-14;
if (x == 0) return fabs(y) <= EPSILON;
if (y == 0) return fabs(x) <= EPSILON;
return fabs(x - y) / max(fabs(x), fabs(y)) <= EPSILON;
}
Syntax 5.3: return statement return expression;
|
b = future_value(total / 2, rate, year2 - year1);
double future_value(double initial_balance, double p, int n)
{ p = 1 + p / 100;
double b = initial_balance * pow(p, n);
return b;
}
print_time(now);
void print_time(Time t)
{ cout << t.get_hours() << ":";
if (t.get_minutes() < 10) cout << "0";
cout << t.get_minutes() << ":";
if (t.get_seconds() < 10) cout << "0";
cout << t.get_seconds();
}
void raise_salary(Employee& e, double by)
{ double new_salary = e.get_salary() * ( 1 + by / 100);
e.set_salary(new_salary);
}
Syntax 5.4: Reference Parameters type_name& parameter_name
|
Syntax 5.5: Constant Reference Parameters const type_name& parameter_name
|