### 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!

 ``123456789101112131415161718192021222324252627`` ``````#include #include 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...
 ``12`` `````` return d; cout << d << endl; ``````

Anything after the return statement does nothing...
`double d;`

and fixed the
 ``12`` ``````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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include #include 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.