Functions an return values

I have been working on this program to calculate the Kinetic Energy of mass at velocity. I am having problems with my return value from the function to the main. I got the passing from main to function. Here is the code, can anyone tell me what im doing wrong?

It gives me an error that kenergy variable is being used without initializing. I clearly have initialized it in the main, and the function.

Please, please, please help. I hate that I can't figure this out.




#include <iostream>
#include <cmath>

using namespace std;


double kinetic(double mass, double velocity)

{

double kenergy;
kenergy = 0.5 * mass * velocity * velocity;

return (kenergy);

}

int main ()

{
double kenergy;
double velocity;
double mass;

cout<<"Please enter the mass of an object in kilograms: ";
cin>>mass;

cout<<"Please enter the velocity of an object in meters per second: ";
cin>>velocity;

kinetic(mass, velocity);

cout<<"The Kenetic Energy for "<<mass<<" kilos, at "<<velocity<<" meters per second is; "<<kenergy;

system ("pause");

return 0;

}
You need to receive a return value into something if you want to keep it. It's not enough to define it and declare it, you have make an assignment into the receiving variable.

kenergy = kinetic(mass, velocity);

Think of it like a math function: y = f(x)
Last edited on
but kenergy is calculated in the function, and Im trying to return its calculated value. I don't need velocity or mass returned to the main.
Im not arguing that it works, i see that it does, i just can't wrap my head around it, and that means more that having the answer. Oh yeah, thanks for the answer. lol
In your program you have two different variables that have the name kenergy.

The kenergy in kinetic is not the kenergy variable in main.

Study scope of variables, local, global,pass by reference etc.
Last edited on
As Roberts said, you need a variable to receive the returned value from kinetic(mass, velocity). The return value from your function is copied into this variable. To reiterate vin's recommendation, read up on variable scope, pass by value, pass by reference etc.

Here's your code with a couple of variable names changed to demonstrate what I mean.


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
#include <iostream>
#include <cmath>

using namespace std;

double kinetic(double mass, double velocity)
{

     double result;
     result = 0.5 * mass * velocity * velocity;

     return (result);
}

int main ()
{
     double kenergy;
     double velocity;
     double mass;

     cout<<"Please enter the mass of an object in kilograms: ";
     cin>>mass;

     cout<<"Please enter the velocity of an object in meters per second: ";
     cin>>velocity;

     kenergy = kinetic(mass, velocity);

     cout<<"The Kenetic Energy for "<<mass<<" kilos, at "<<velocity<<" meters per second is; "<<kenergy;

     system ("pause");

     return 0;
}


You could also do this:

cout<< "The Kenetic Energy for " <<mass<< " kilos, at " << velocity << " meters per second is: " << kinetic(mass, velocity);

You could also do this with your kinetic(...) function:

1
2
3
4
double kinetic(double mass, double velocity)
{
     return (0.5 * mass * velocity * velocity);
}
Topic archived. No new replies allowed.