int main() why does this work with float?

#include "header.h"



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
float mult ( float x, float y, float z );

  int main()

{ 
   float x;
   float y;
   float z;

   cout << "input three numbers to be multiplied:  ";
   cin >> x >> y >> z;
   cin.ignore();
   cout << product of three numbers:  " << mult ( x, y, z ) << "\n";
   cin.get();

   }
   float mult ( float x, float y, float z )
   {

         return  x * y * z
} 


Also what am I doing wrong with the code tags????

Last edited on
[/code]
erm... what is the question?

if your question is why returning an integer when the return value is float just like this function

1
2
3
4
float mult ( float x, float y, float z )
{
     return 0;
}


then the answer is implicit typecasting by your compiler.
But it seems to me that:

int main()

is explicit that an integer be returned not a float value?
Thank you guestgulkan. Silly me!
You aren't returning anything. When you type in return 0; then you are returning '0' as the data type of the function. The variables used inside the function don't really matter in this context in any way I can think of. If in Dacster13's example the value of function would be an int data type if he were returning it, but again return 0;.
Also what seems strange is all that is necessary to change this code from multiplication to Sum or
Subtraction or Division or possibly other mathematical solutions is to simply change signs:

1
2
3

return ( x * y * z ) or ( x + y + z ) or ( x - y - z ) or ( x / y / z ) 
Dacster13,

I changed return 0; to return x * y * z;

does this change alter your response?
Last edited on
BAH! I didn't see that Dacster13 used float mult(...). I'm a fool! But he is still returning 0 as his value so it wouldn't make a differance.

@ultramint: The result of x, y, z will be returned from 'mult' as float data types.
Thank you Computergeek01. What is your input on my last post to Dacster13?
Thanks everyone I believe I see the light :-)
Dacaster13 didn't interperate the question the same way I did. I saw it as you asking why you can use a float value inside of a function that is an int data type. This is because the value of the variables don't matter until one is returned. The function then takes on the value of that function and its data type. So if main() returned anything except '0' and another process was expecting something from it, then that other process would get the data as an int.

No, it shouldn't change his response.
Topic archived. No new replies allowed.