int vs double | Sololearn: Learn to code for FREE!

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)?

7/23/2020 10:55:23 AM

Solus

3 Answers

New Answer

+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.

+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?

+1

Solus Yup :)