+ 3
Could someone explain this to me??
{ int foo(int x , int y) res = (x+y)} { int foo(double x , double y) res = (x*y) } int main(){ cout<<foo(3.8,8.0) }
9 Réponses
+ 12
What is your doubt?
PS. The code has errors.
+ 12
the compiler decides what function to use depending in the type of the arguments.
In the example, the arguments are of type double, this means the second foo function is used.
The result will be the multiplication of the arguments.
+ 11
Do you mean: how to make it work?
+ 11
Could you, please, post the code in the Code Playground so we can see it better?
+ 1
how to do it? i just can't understand it & yeah it have! but i think you could still do it?
+ 1
process behind the output
+ 1
its done thank you :)
0
its function overloading . but there is an error in your code... it goes like..
int foo(int x, int y)
{
sum= x+y;
}
double foo(double x, double y)
{
result=x*y;
}
int main()
{
cout<<foo(3.8,3.0);
}
0
look the working example of function overloading
#include <iostream>
using namespace std;
void foo(int a, int b)
{
int sum=a+b;
cout<<"Sum :"<<sum;
}
void foo(double a, double b)
{
double res=a*b;
cout<<"Product is "<<res;
}
int main() {
foo(3.8,8.0);
return 0;
}