Difference Between Operator Overloading & Function Overloading


Operator overloading and functional overloading are two familiar terms that we have in computer programming. These terms are specific cases of polymorphism. 

If you haven’t heard about polymorphism yet, polymorphism is an object-oriented programming concept in which objects belonging to different classes can respond to the same message but in different ways. In simple terms, polymorphism allows using the same operator or function and provides many different outcomes.

I’ve written an in-depth guide about object-oriented programming in python. If you lack the basic oops knowledge, check out that article, and you can come back to this article later. It will give you a much better understanding of what I’m talking about in this article.

If you already know the basics of object-oriented programming, then let’s move ahead to the meat of this article.

You might have heard about these terms a lot, especially related to object-oriented programming. Let’s see what they are and their differences.

What is the difference between operator overloading and functional overloading? Operator overloading allows operators to have an extended meaning beyond their predefined operational meaning. Function overloading (method overloading) allows us to define a method in such a way that there are multiple ways to call it.

It is possible to do a declaration with the same name as a previously declared declaration in the same scope, which is called an overloaded declaration. This type of declaration can have different arguments and obviously different implementations.

When we call an overloaded declaration, the compiler finds out the exactly appropriate function to use, based on the argument types you have used in the function call. The process of selecting the suitable overloaded function or operator is called overload resolution. This is how overloading works in most programming languages.

Now let’s dive deep into both these concepts to learn more about them. Let’s start with operator overloading.

Operator Overloading

Operator overloading is a programming concept where different operators have different implementations depending on their arguments. It is the mechanism of giving a special meaning to an operator. Operator overloading allows operators to have an extended meaning beyond their predefined operational meaning.

Operator overloading is a kind of syntactic sugar. In programming, syntactic sugar is syntax within a programming language that makes things easier to read or express. It makes the language sweeter for human use.

We can overload all existing operators in a programming language, but we can’t create a new operator.

Python provides some special functions called magic functions for each operator that is automatically invoked when it is associated with that particular operator. For example, if we are overloading the “+” operator, the magic method __add__ is automatically invoked in which the functionalities of the + operator are defined.

For example, we can add two integer values using the + operator. We can also add two string values as well. But, we can’t use the + operator to add an integer with a string. If we do so, the compiler will throw an error because it doesn’t know how to add two objects. But, we can define a method for this operator and then use the operator.

To define an additional task to an operator, we must specify what it means to the class to which the operator is applied. We can do it with the help of defining a special function, which is called the operator function.

You can define operator methods in your class, and operators work according to that behavior defined in methods.

In Python, operator functions are the same as normal functions. But in some other languages, there are some differences in the syntax.

Enough of the theory, let’s see some Python code to understand everything.

Let’s say we have an integer value and a string value.

a = 3
b = 'apples'

We want to add these two variables together. But, when we use the + operator, it will cause an error.

print(a+b)

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

So, we need to define an operator function for +.

class Apple:
    def __init__(self,x):
        self.x = str(x)
    def __add__(self,y):
        return self.x + y.x
obj1 = Apple(3)
obj2 = Apple('apples')
print(obj1+obj2)

Here in this example, we defined a special function by converting the integer value to string within the function. Now, when we run this code, we will not get the error. Instead, we will get the concatenated values as the output.

In the above example, the output will be:

3apples

In this way, we can give a special definition to almost all the operators (except a few) in Python.

Function Overloading

As we have seen, overloading means the use of the same thing for different purposes. In function overloading, we can define two or more functions with the same name and in the same scope.

We can define a family of functions with one function name but with different argument lists. The function would perform different operations depending on the argument list in the function call. This feature is commonly known as method overloading, as we call functions as methods in object-oriented programming.

You can define a method in such a way that there are multiple ways to call it. Depending on the function definition, it can be called with zero, one, two, or more parameters.

The overloaded member functions could be invoked by matching arguments, specifically, the number of arguments and their data types.

In most programming languages, you can create multiple functions having the same name, but different parameters. However, we can’t create multiple functions with the same name in Python.

So, what we do is, we’ll do the overloading within one function. It is done by using the feature called default function parameters. By default, we set the parameters of the function as None. So, the overloading concepts will work just fine in Python as well.

The compiler knows this information at compile time. Therefore, the compiler can select the appropriate function for a particular call at the compile time itself. It is called compile-time polymorphism (or early binding or static binding).

Let’s see an example Python program to understand the concepts of method overloading.

class Greetings:
    def Hello(self, name=None):
        if name is not None:
            print('Hi ' + name)
        else:
            print('Hi')
obj = Greetings()
obj.Hello()
obj.Hello('Ashwin')

As you can see in this example, the same function Hello() is used for different purposes. The first parameter of this method is set to None. So, we can call it with or without an argument.

Since we can’t create multiple functions with the same name in Python, we used the default parameter as None.

If we call the function with or without an argument, the compiler can distinguish what was intended to be used at each occurrence.

Sometimes, people often get confused with method overloading and method overriding. The latter is a completely different feature in polymorphism.

Overloading occurs when multiple methods in one class have the same name, but different parameters. Overriding occurs when there are two methods with the same method name and parameters. One of those methods is in the parent class, whereas the other is in the child class.

Conclusion

In this article, we have seen the basic concepts of operator overloading and function overloading along with example Python programs.

We can specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading, respectively.

If you have any doubts regarding the concepts that we have discussed, feel free to put your comments down below. I’ll be happy to help you.

I hope you got a clear idea about operator overloading and function overloading. Check out this article to understand the difference between method overloading and method overriding.

I would appreciate it if you would be willing to share this article. It will encourage me to create more useful articles like this.

Happy coding!

Ashwin Joy

I'm the face behind Pythonista Planet. I learned my first programming language back in 2015. Ever since then, I've been learning programming and immersing myself in technology. On this site, I share everything that I've learned about computer programming.

2 thoughts on “Difference Between Operator Overloading & Function Overloading

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts