Click here to Skip to main content
15,887,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I know this is very basic & silly question but if anyone can clear me on this. As overloading says, same name different functionality..My question is...
suppose we have two overloaded functions

Add( int a, int b);
Add( float a, float b);

so i would like to know here is. Why should i overload a function?
i can give a different name to both the functions like

AddIntegers( int a, int b);
AddFloats( float a, float b);

I know the things that will come in our mind after reading this are:
How far we can give different name to different functionality.
How do we maintain a uniformity.

But i believe other than this there should be any reason to introduce overloading concept.
Posted

It's not a silly question: it is a fundamental question.

The second way you describe is exactly how we used to do it, before OOP.
Every time you needed a slightly different set of parameters, you needed a new function name.

It's ok, when you have just "int" and "float", but whet happens when your overloads get more complex:
Add(int x1, int y1, int x2, int y2)
Add(Point p1, Point p2)
Now you need some slightly complicated names.

What about:
public static void Email(string body)
public static void Email(string body,
                         params MailAttachment[] attachments)
public static void Email(string to, string body)
public static void Email(string to,
                         string body,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject)
public static void Email(string to,
                         string body,
                         string subject,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         params MailAttachment[] attachments)
public static void Email(string to,
                         string body,
                         string subject,
                         string fromAddress,
                         string fromDisplay,
                         string credentialUser,
                         string credentialPassword,
                         params MailAttachment[] attachments)
(Which I just ripped out of my email utils file).
What do you start calling each one? And how do you remember which one you need?

Basically, they make the code more readable, and make the logical flow simpler.
 
Share this answer
 
Comments
AmbiguousName 7-Mar-11 5:27am    
Good one. After reading the question, for a second I myself forgot what I was told in the class room 3 years back. But after thinkg for 1 minute I got it and your answer confirmed it.
Albert Holguin 8-Mar-11 10:46am    
the Email() example you gave is great! my 5!
Overloading allows you to define a different method signature, so you to pass many different parameters or data types to a method,

C#
public void OverloadTest()
{
    OverloadTest(0, string.Empty);
}

public void OverloadTest(int a)
{
    OverloadTest(a, string.Empty);
}

public void OverloadTest(string b)
{
    OverloadTest(0, b);
}

public void OverloadTest(int a, string b)
{
   Dosomething(a, b);
}


Notice in the above, only one function will actually do anything with the parameters (Dosomething). All of the other methods just call the 'greediest' method signature and pass default values for any 'missing' parameter values.

Because of this syntax, the following works OK

C#
int a = 0;
OverloadTest(a);

string b = "hello";
OverloadTest(b);

OverloadTest(a, b);


A good real life example is the ToString method, where we can pass all sorts of Data Types to the method and it will work OK

C#
int x = 10;
Console.WriteLine (x);

string Message = "Hello";
Console.WriteLine (Message);
 
Share this answer
 
v2
Suppose you have a class called Student

You want to be able to create an instance of Student.
You can over load the default constructor like the following:
public Student()
{
}

public Student(string name)
{
}

public Student(int age)
{
}

public Student(string name, int age)
{
}


The above code gives the flexibility to set the properties in the class either by passing them from the constructor (that is if you know them before hand) or you can set them later on. The flexibility here is that you would not be required to fill the parameter when you are creating the instance of the Student class
 
Share this answer
 
1->
C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types
2->The C++ compiler identifies a function by its signature (function signature). The function signature is broken down into the following components in that order.
<return type>
<function label or name>
<parameters list>

The parameter list is further defined as <data type> parameter label or name. If the order of the parameters (datatype name) changes, even if the function name/label does not change, the signature is considered unique. So, in C++, it is conceivable to have many different functions/methods with exact same label name (for example Function1) so long as the entire signature is considered unique. So, changing the number of parameters, their data types, the order of them, or any combination of these can change the function signature.
Important Note: Changing the return type ALONE is NOT considered change in signature.
 
Share this answer
 
Comments
LaxmikantYadav 8-Mar-11 6:27am    
In function Overloading return type does not play any role.
Himansu sekhar bal 9-Mar-11 4:01am    
in function overloading argument list must be difference
Niklas L 8-Mar-11 18:33pm    
...while instance method const-ness does play an important role.
LaxmikantYadav 10-Mar-11 23:25pm    
Can you please explain with the example.
Niklas L 11-Mar-11 4:15am    
const A* Get() const;
vs
A* Get();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900