Which function will be called in function overloading double/float? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Which function will be called in function overloading double/float?

If you declare two fuctions sum(double x,double y) and sum(float x,float y) to sum two values which fuction will be called through function overloading when you input two decimal values(ex. 12.6,4.5) Do upvote guys❤

18th Nov 2017, 9:24 PM
Drumsticks
Drumsticks - avatar
7 Answers
+ 1
double is your default floatingpoint datatype, and integer is your default number datatype. if you write float a = 12.4; the compiler actually casts the 12.4 from double to float. if you write: long a = 1034; the compiler casts the 1034 from int to long. (byte and short are treated as integers internally anyways) oh and: all this is java based.
19th Nov 2017, 1:50 PM
Jeremy
Jeremy - avatar
+ 3
That can be taken eithet as float or double value.so what now??
18th Nov 2017, 10:56 PM
Drumsticks
Drumsticks - avatar
+ 3
nope, those are doubles. if you want them to be float you would have to write 12.6F and 4.5F
18th Nov 2017, 11:07 PM
Jeremy
Jeremy - avatar
+ 3
so is double the default data type for decimal values? Cause even to specify it is double i have to mention it as 12.6d &4.5d It is only possible if double is taken as default.
19th Nov 2017, 11:45 AM
Drumsticks
Drumsticks - avatar
+ 3
@jeremy Is it the same way in c/cpp?
19th Nov 2017, 1:52 PM
Ran Avital
Ran Avital - avatar
+ 2
you are passing double values, therefore the method with the double parameters is used.
18th Nov 2017, 10:17 PM
Jeremy
Jeremy - avatar
+ 1
im not 100% sure but i think so. i just tried to store 12.4 in a float in c# and i received and error that you cant store a double in a float. No idea about cpp, you have to try it i guess.
19th Nov 2017, 1:55 PM
Jeremy
Jeremy - avatar