int vs double | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

int vs double

int function(double x) { if(x>0) { function(x-2.5); } else { return x; } } int main() { cout << function (20.0); } // output is 0 What is the reason for the 0 output? I have a gut feeling that the int and double in the first line have to be one or the other. But if that's the case, why would the compiler even pump out a result (and not an error)?

23rd Jul 2020, 10:55 AM
Solus
Solus - avatar
3 Answers
+ 1
Solus Well , you are subtracting x by 2.5 until it is 0 or less than 0 and returning the x. Try it yourself in a calculator and keep on subtracting 2.5 from 20. You will land on 0 eventually. An 'int' function will return an integer. So if x = -1.4641 it will return -1; A 'double' will return -1.4641.
23rd Jul 2020, 11:22 AM
Ćheyat
Ćheyat - avatar
+ 1
So an int type function can have parameters that have different number data types (like float and double)? And as long as the int type function's final output is without decimal points, everything is okay?
23rd Jul 2020, 11:37 AM
Solus
Solus - avatar
+ 1
Solus Yup :)
23rd Jul 2020, 11:50 AM
Ćheyat
Ćheyat - avatar