How do I used a calculated value from one function as a variable for another function?

I have to calculate v=d/t in one function and then use the answer v to calculate k= 0.5mv2 in another function with d,t and m being entered from the keyboard. The first function is outputting the v value just fine, but the second function shows "nan" as the output.

This is the code I have so far

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


double CalcVel(double d, double t, double v);
double CalcKin(double m, double v, double k);

int main(){
	
	double d, t, m, k, v;
	cout<<"Enter distance, time and mass"<<endl;
	cin>>d>>t>>m;
	
	v=CalcVel(d, t, v);
	cout<<"The velocity is "<<v<<endl;
	
	k=CalcKin(v, k, m);
	cout<<"The kinetic energy is "<<k;
	
	
	
	return 0;
	
	
}



double CalcVel(double d, double t, double v)
{
	

	v=d/t;
	
	
	return v;
}

double CalcKin(double m, double v, double k)
{
	
	k=(1/2)*m* pow(v, 2);

	return k;
}
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
#include <iostream>

double velocity( double distance, double time );
double kinetic_energy( double mass, double velocity );

int main(){

	double distance, time, mass;
	std::cout << "Enter distance, time and mass\n" ;
	std::cin >> distance >> time >> mass ;

	const double vel = velocity( distance, time ) ;
	std::cout << "The velocity is " << vel << '\n' ;

	std::cout << "The kinetic energy is " << kinetic_energy( mass, vel ) ;
}

double velocity( double distance, double time ) { return distance/time ; }

double kinetic_energy( double mass, double velocity ) {

    return 0.5 * mass * velocity * velocity ;

    // or return (1/2.0) * mass * velocity * velocity ;
    // note: avoid integer division eg. 1/2
}
Thank you so much. I got it to work now.
Topic archived. No new replies allowed.