0

Function overloading - weird number returns when return is not provided. what is this number?

int sum(int a, int b=23) { int result = a*b; return(result); } int sum(double a, int b=23) { cout << "second func"<<endl<<a<<endl; //no operation no return } int main() { cout<<"sum with double as parameter "<<endl<<sum(2.01f)<<endl; } Output: second func 2.01 sum with double as parameter 4757824 where is the number 4757824 coming from? also when i use float sum(double a, int b=23) { cout << "second func"<<endl<<a<<endl; //no operation no return } and passing 2.01 as argument for 'a' i am not getting 25.01 as output, what am i missing?

4th Sep 2017, 9:48 AM
rakesh c
rakesh c - avatar
5 Réponses
+ 8
.. because you are referencing an unitialized value when you remove the return statement from sum int sum() should return an integer, if it returns nothing a random value is accessed from that memory location it is in effect the same as doing: int a; cout << a;
4th Sep 2017, 10:04 AM
jay
jay - avatar
+ 3
Use double instead float
4th Sep 2017, 10:01 AM
Kartikey Sahu
Kartikey Sahu - avatar
4th Sep 2017, 10:18 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 1
If you do not intend to return an intager your code should be "void sum(prarams) {... }
4th Sep 2017, 10:20 AM
josh mizzi
josh mizzi - avatar