0
Below is the code, when I am executing. No output in the console. Not sure why.
I am testing one example of function overloading in C++.

Function Overloading. Created 2 functions of the same name with different arguments. When tried to pass integer value in main during the function call. It worked fine. But when passed as float number, neither error nor output is coming.

#include <iostream>

using namespace std;

int max(int x,int y)
{
    cout<<"Entered in max int"<<endl;
    if(x>=y)
        return x;
    else
        return y;
}
float max(float x,float y)
{
    cout<<"Entered in max float"<<endl;
    if(x>=y)
        return x;
    else
        return y;
}
int main()
{
    float x;
    x=max(5.9,6.7);
}
  • Because of [`std::max`](https://en.cppreference.com/w/cpp/algorithm/max). Get rid of [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – G.M. Jan 28 '21 at 06:27
  • [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Jan 28 '21 at 06:38

2 Answers2

0
  • there is some implicit conversion (float to int), when call float type max function.So you need specify that.
max(5.9f, 6.7f)
KennetsuR
  • 704
  • 8
  • 17
0

First you are not outputting the result of the function and 2nd your function is not being called because you are using using namespace stdso when you call max std::max is being called. To call your version ofmax you have to access the global scope. Like this ::max(x, y).

foragerDev
  • 1,307
  • 1
  • 9
  • 22