### Passing arguments by-Value and by-Reference

I'm getting an error that says "meter is being used with being initialized". I am converting the void function converToMeters to a double. I might have done that wrong.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081`` ``````#include using namespace std; // Constant factor converting feet to meters: 3.28 feet = 1 meter const double FTM_FACTOR = 0.304878048; // (1 / 3.28) // TODO: Modify convertToMeters function declaration so that it is // a "value returning" function instead of a void function. void getFeetAndInches(int& ft, double& in); double convertToMeters (int ft, double in); void displayResults (int ft, double in, double m); // ******************************************** int main() { // Declare program variables. int feet; double inches, meters; getFeetAndInches(feet, inches); // TODO: Modify convertToMeters function call statement // as necessary for a value returning function. convertToMeters(feet, inches); displayResults(feet, inches, meters); return 0; } // ******************************************** void getFeetAndInches(int& ft, double& in) { cout << "Enter feet: "; cin >> ft; cout << "Enter inches: "; cin >> in; } // ******************************************** // TODO: Modify convertToMeters as necessary so that it is // a value returning function intead of a void function double convertToMeters(int ft, double in) { double m; // convert feet to fractional feet double fractionalFeet = double(ft); // convert inches to feet, then add to fractional feet fractionalFeet = fractionalFeet + (in / 12.0); // return converted feet through reference parameter m = fractionalFeet * FTM_FACTOR; return m; } void displayResults(int ft, double in, double m) { // Display formatted output cout << endl << ft << "'-" << in << "\" = " << m << " meter(s)." << endl; }``````
Line 29, the function is called. It returns a double value as the result. What happens to that result? It should be assigned to some variable.
I don't follow. I converted inches and feet, stored that number in the double variable "m", and returned "m". Would I assign "m" to "meters"?
 I converted inches and feet, stored that number in the double variable "m", and returned "m"....
and you didn't do anything with it. You threw it away. Here is how to assign the return value to something.

 ``12`` ``````double aVariable; aVariable = convertToMeters(feet, inches);``````

Now the variable named "aVariable" holds the returned value from the function.

No, m exists only inside the function.
In main(), where you call the function, it is the function itself which represents the value it has returned.
Thus `meters = convertToMeters(feet, inches);`

I think you should review the tutorial on functions here: http://www.cplusplus.com/doc/tutorial/functions/

Wow, I'm not sure how I missed that, because it's right in my notes...Thanks guys
Topic archived. No new replies allowed.