Matt Posted January 13, 2005 Report Share Posted January 13, 2005 The following Source Code code snippit came from a sample from the book 'C++ for Dummies.' The areas marked in color are the areas I have questions about. The questions are listed below // 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 doubleVardouble 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 numberdouble 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@ Quote Link to post Share on other sites
Codemuffin Posted January 13, 2005 Report Share Posted January 13, 2005 Hey there. 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? It works because you are passing dValue 's information to square(), which will perform the necessary calculation. Different variables can be used for the same action because when you define a function, you can use anything between those parentheses. For example:double cube(double someNumber){return someNumber * someNumber * someNumber;}// main block int main {double someNumValue = 5.0; cout << "The cube of " << someNumValue << " is " << cube(someNumValue) << endl;return 0;}It should print out to the screen: The cube of 5 is 125. Of course, we could always move away from hard-coding the number in (note that in an application, we always need to make sure the user can enter their own numbers). The point is: all you're doing with these functions is passing in a value. We can use anything to define the function's parameters, so long as the variables we pass to the function match the specifications (in both examples, the variable needs to be a double) . Hope that helps,Codemuffin Quote Link to post Share on other sites
Matt Posted January 13, 2005 Author Report Share Posted January 13, 2005 Thanks, you answered everything I needed help with. The book didn't say that you could use different variables, as long as they matched the specifications. I guess the book may leave alot more out later. Don't be surprised if I'm in this forum alot!Thanks again codemuffin.M@ Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.