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?
5 Réponses
+ 8
Your bestestest friend: Undefined behaviour.
https://stackoverflow.com/questions/3459810/how-do-c-progs-get-their-return-value-when-a-return-is-not-specified-in-the-f
+ 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;
+ 3
Use double instead float
+ 3
https://code.sololearn.com/cqbd2v6Vidmi/?ref=app
Look at this
+ 1
If you do not intend to return an intager your code should be
"void sum(prarams) {... }