// SquareDemo - demonstrate the use of a function
// which processes arguments
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
// square - returns the square of its argument
// doubleVar - the value to be squared
// returns - square of doubleVar
double square(double doubleVar)
{
return doubleVar * doubleVar;
}
// sumSequence - accumulate the square of the number
// entered at the keyboard into a sequence
// until the user enters a negative number
double sumSequence(void)
{
// loop forever
double accumulator= 0.0;
for(;
{
// fetch another number
double dValue = 0;
cout << "Enter next number: ";
cin >> dValue;
// if it's negative...
if (dValue < 0)
{
// ...then exit from the loop
break;
}
// ...otherwise calculate the square
double value = square(dValue);
The code does continue, but this is the part I need help on. The program does what it is intended to: add and square numbers. My questions are:
The first part of the function says it deals with the variable square(double doubleVar). Yet, the second part deals with the variable dValue . These are different variabes, but refering to the same function. Why does it work ( as in calculate the square) even though the variables are different? Can different variables be used for the same action? Should dValue be doubleVar? Am I just missing something?
Thanks,
M@

Sign In »
Register Now!
Help

Back to top
MultiQuote