Everything in C++ has a type. Not all things need have the same type.
A function's
return type is only the type of value it returns. Currently your
Carspeed
function does not
return any value, so it’s return type is, correctly,
void
. (It
modifies the value of two
reference arguments, sometimes called something like “out” arguments.)
According to your apparent instructions you are doing it correctly.
But in real code, you should try to have a function do one thing instead of multiple things. I do not know where 3.6 comes in to your equation, but the
speed of something is (distance ÷ time), hence:
t=5, d=20 → 20/5 = 4
If we were talking, say, kilometers per hour, then 20 kilometers ÷ 5 hours = 4 kph.
You could write a simple function:
1 2 3 4 5
|
double speed( double distance, double time )
{
if (time == 0.0) return 0.0;
return distance / time;
}
|
And use it thus:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
double d1, d2, t;
std::cout << "distance 1? ";
std::cin >> d1;
std::cout << "distance 2? ";
std::cin >> d2;
std::cout << "time? ";
std::cin >> t;
std::cout << "speed 1 = " << speed( d1, t ) << "\n";
std::cout << "speed 2 = " << speed( d2, t ) << "\n";
|
Hopefully this helps in your thinking. The assignment seems to be to use reference arguments with a void function so you are doing OK, as far as I can see.
Hope this helps.