Return Type

Dec 4, 2008 at 2:03am
Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int max(int a, int b)
{
     if (a > b)
        return a;
     else
        return b;
}

int add(int a, int b)
{
    return (a+b);   
}

int main()
{
    cout << add(2.5,3.5) << endl;  // this returns 5, correct
    cout << max(2.5, 3.5) << endl; // this should returns 3, but 3.5, why?
 
    
    return 0;
   
}


My Question is on the following both line:

1
2
cout << add(2.5,3.5) << endl;  // this returns 5, correct
cout << max(2.5, 3.5) << endl; // this should returns 3, but it returns 3.5, why? 



Dec 4, 2008 at 2:05am
it probably returns 3.5 because that's the parameter you're feeding into max...
Dec 4, 2008 at 2:07am
can you explain me more detail. Buy why it is different from add function
Dec 4, 2008 at 2:12am
I really don't know how to explain this in more detail. Why do you think your code should return 3, when the two numbers that it has to deal with are 2.5 and 3.5? Do you understand what is going on in max() and add()?
Last edited on Dec 4, 2008 at 2:13am
Dec 4, 2008 at 2:29am
Huh...
I agree with OP. I should return 3 with the implicit type conversion. I don't know why it doesn't.

EDIT: Oh. I think I found it. It must be conflicting with the existing double max(double,double) function which iostream includes. The compiler automatically chooses the most appropriate function and calls that.
Try renaming your function to anything else and it should work.
Last edited on Dec 4, 2008 at 2:31am
Dec 4, 2008 at 2:33am
OH drat, my bad. Yeah, that's it. heh, lolfail @ me.
Dec 4, 2008 at 3:19am
Haha - i probably would of missed that as well :D
Dec 4, 2008 at 3:21am
Yes, great helios, it conflict!
Topic archived. No new replies allowed.