0
Can anyone please explain the calculation procedure?
int foo(int x, int y) { return x + y; } int foo(double x, double y) { return x * y } int main() { cout<<foo(3.8,8.0); } I know the result is 30 but I can't figure out exactly how it works. Thanks :)
4 Respostas
+ 5
Briefly, arguments 3.8 and 8.0 are not integers.So, second function works. 3.8*8.0=30.4
But function data type is integer
So, result is 30
if double foo(double x, double y)
result will be 30.4
If arguments are integer like (3,8), first function will work , 3+8 and the result will be 11
I am explaining in easier way,the another one who has deeper knowledge might explain you in deeper way.
+ 5
you called a function where parameters have decimal places so it calls the second foo function to return those two doubles but in interger so decimal places got lost(some compliers will warn you about this) since you specify the type int.
+ 3
In main you call the (second) foo function with the arguments 3.8 and 8.0. The function is a simple multiplication. So the result is 3.8 * 8.0 = 30.4. Since you specify the type as int that result is truncated to 30.
+ 1
Thanks.