Return Type

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? 



it probably returns 3.5 because that's the parameter you're feeding into max...
can you explain me more detail. Buy why it is different from add function
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
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
OH drat, my bad. Yeah, that's it. heh, lolfail @ me.
Haha - i probably would of missed that as well :D
Yes, great helios, it conflict!
Topic archived. No new replies allowed.