Next: Example Function: Raising to
Up: Introduction to User-defined functions
Previous: Functions that return values
The following function returns the sum of the squares of the first n
integers when it is called with parameter n.
// This function returns the sum of squares of the
// first n integers
int sumsq(int n)
{
int sum = 0;
int i;
for (i = 1; i <= n; i++)
sum += i * i;
return sum;
} // End of sumsq
Download program.A typical use of
sumsq is:float sumsquare; int number; cout << "Enter number (>= 0): "; cin >> number; sumsquare = sumsq(number);