Function Help

Hello,

I'm trying to get this function to work where it asked the user to input a value for temp, and then calculates falling distance by using the following formula: d=0.5*g*t^2.

I keep getting a warning "warning C4700: uninitialized local variable 'd' used"

Then an error to abort stating "The variable 'd' is being used without being initialized"

I don't understand this since I have 'd' initialized in both the function and the call, well at least I think I do.

Any help would be appreciated!

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
#include <iostream>
#include <cmath>
using namespace std;

const double g = 9.8;

double calcFalldistance(double d)
{
	double t;
	d = (0.5 * g * pow(t,2));
	return d;
	cout << d << endl;
}


int main()

{
	double t;
	double d;
	cout << "Please enter time" << endl;
	cin >> t;
	
	calcFalldistance(d);

		
}
You Never Declared 'd'

 
double d;


And another thing...
1
2
	return d;
cout << d << endl;	


Anything after the return statement does nothing...
I added
double d;

and fixed the
1
2
cout << d << endl;
	return d;
but now im getting a "redefinition of formal parameter 'd'"
In your main function, you declared d, but you didn't initialize it with a value. You give this uninitialized variable to your calcFallDistance function, which will make the compiler complain.

In your calcFallDistance function, you declared t, but did not initialize it with a value. Then you try to take the square of the uninitialized variable, which will make the compiler complain.
Your function can only return, you can't cout. Else use a void function.
Ok so I fixed everything and get the code to compile and give me the results i want. My next question is, how do I get rid of the global variables? I'm not sure what to do.

Where do I have to put these
 
double d, t, m, v, k;

to make it work locally for each function?

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
#include <iostream>
#include <cmath>
using namespace std;

const double g = 9.8;
double d, t, m, v, k;

double calcFalldistance(double d)
{

	cin >> t;
	d = (0.5 * g * pow(t,2));
	cout << "" << endl;
	cout << "The falling distance of the object is " << d << endl;
	cout << "" << endl;
	return d;	
}

double calcKin(double k)
{
	cin >> m >> v;
	k = (0.5 * m * pow(v,2));
	cout << "" << endl;
	cout << "The kinetic energy is " << k << endl;
	cout << "" << endl;
	return k;
}


int main()

{
	int num;

	do
	{
	cout << "Please Choose from the following:" << endl;
	cout << "" << endl;
	cout << "      1. Calculate Falling Distance" << endl;
	cout << "      2. Calculate Kinetic Enegery" << endl;
	cout << "      3. Quit" << endl;
	cout << "" << endl;

	cin >> num;
	
	if (num == 1)
	{
		cout << "" << endl;
		cout << "Please enter the objects falling time in seconds" << endl;
		cout << "" << endl;
		
		calcFalldistance(d);
	}

	else if (num == 2)
	{
		cout << "" << endl;
		cout << "Please enter mass in kilograms and velocity in meters per seconds" << endl;
		cout << "" << endl;

		calcKin(k);
	}

	}while(num < 3);
			
}
Last edited on
I've actually figured it out!

Thanks everyone for the help!
Topic archived. No new replies allowed.