Just wanted to give some more information on exactly what was happening and why it was converting it to a integer.
Now when you pass or return a variable from a function by value (Meaning that you don't use a reference or pointer) the function creates a copy of whatever you pass in.
So for example lets say we have this function which adds two numbers together and returns the sum.
1 2 3 4
|
int addTwo(int one, int two)
{
return one + two;
}
|
And we call the function like this
int sum = addTwo(10, 5);
What our function is doing is first it is creating two temporary variable called
one and
two and assigning the values 10 and 5 to them respectively. So we aren't actually working with the actual values we pass in we are essentially working with copies of them. So remember when we assign a double to a int it implicitly converts that double to a integer and then stores it.
So with that said the same goes for return types. When we return by value from a function the function is creating a temporary variable and then assigns whatever we are returning to that variable (In this case it is the sum of two numbers). Then it it takes that temporary variable and assigns it to the variable
int sum
in our example.
So that is why it was returning a integer instead of a double. Even though you might have passed a double into the function it would still return as a integer because when the temporary return variable was created it converted the double into a integer so that it could return it.
This is always why pass by reference is so important sometimes, because as you can imagine if we had a very complex and large type we were passing to a function it would take away a good chuck of performance to make all them temp variables and copies. Especially when that function might be used recursively.
Hope that might help shed some light and was understandable ;p