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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
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.

1
2
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.