Question

Are the following two functions overloaded? int xyz(int x, int y = 100000); int xyz(int x,...

Are the following two functions overloaded?

int xyz(int x, int y = 100000);

int xyz(int x, int y);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

No this is not the case of function overloading, function overloading is done on the basis of 'type of arguments' and not on the basis of value of argument.

int xyz(int x,int y=100000); //function declaration

This is known as function with default argument, helpful in case when the user just passes one(here in this case) argument while calling the function

For example:

#include<iostream>
using namespace std;
int xyz(int x, int y=100000)
{
   cout<<(x+y);
}

int main()
{
   xyz(200000); //will take x=200000 and y is by default assigned during function declaration
}

output:

But if we also declare another function in the same program, instead it will show an error stating function already defined because as said earlier function overloading is done on the basis of type of argument not on the basis of value of argument

#include<iostream>
using namespace std;
int xyz(int x, int y=100000)
{
   cout<<(x+y);
}
int xyz(int x, int y)
{
   cout<<(x-y);
}
int main()
{
   xyz(200000,500);
}

Add a comment
Know the answer?
Add Answer to:
Are the following two functions overloaded? int xyz(int x, int y = 100000); int xyz(int x,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT