Next: Call-by-value parameters
Up: Introduction to User-defined functions
Previous: Example function: sum of
float power(float x, int n)
{
float product = 1.0;
int absn;
int i;
if ( n == 0)
return 1.0;
else
{
absn = int(fabs(n));
for (i = 1; i <= absn; i++)
product *= x;
if (n < 0)
return 1.0 / product;
else
return product;
}
} // end of power
Download program.A typical use of the
power function is shown belowfloat x, y; int p; cout << "Enter a float and an integer: "; cin >> x >> p; y = power(x, p); y = power(x + y, 3);
Download program.