Internet
Fact-checked

At EasyTechJunkie, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.

Learn more...

What is Function Overloading?

Jessica Susan Reuter
Jessica Susan Reuter

Function overloading is a feature of many programming languages where multiple methods can be created with the same name but with different input parameters or return types. This is a common feature in object-oriented programming; constructors in particular may have multiple different overloaded variants. Essentially, function overloading is useful when two functions do very similar things and can take multiple types of input to do so. Constructors can be overloaded easily because they have no explicit return type specified, so simply changing the input parameters of a constructor would be valid overloading.

The first way in which a function can be overloaded is by changing its input parameters. For example, one can consider a function called "add" that takes as its input two whole numbers. The purpose of this function is fairly obvious; it is meant to find the sum of two whole numbers.

In object-oriented computer programming, when two or more functions share the same name but have different parameters it is called function overloading.
In object-oriented computer programming, when two or more functions share the same name but have different parameters it is called function overloading.

If one wanted to find the sum of two floats, or decimal numbers, however, this function would not be applicable. So, a second overloaded function would be necessary, defined as a function called "add" that takes as its input two floats. The functions do essentially the same thing, but function overloading is necessary because their inputs are significantly different.

The second way in which a function can be overloaded is to change both its input parameters and its return type. Using the previous example, one can consider if both overloaded "add" functions had a void return type; that is, they returned nothing. This would still be valid function overloading, but the return types of the two methods could also be changed. In another valid instance of overloading, the "add" function that takes two whole numbers could return that whole number sum, and the "add" function that takes two decimal numbers could return that decimal number sum.

Simply changing the return type of a function, however, is not considered valid function overloading. Changing only the return type of a function results in ambiguity, because it isn't clear from the method signature which return type is desired. Parameters can also be defined ambiguously, so it is very important for overloaded functions to be called properly. For example, if one has an overloaded function that has a whole number as its input parameter, and another that has a decimal number as a parameter, it would be very easy for someone to accidentally call the decimal number function instead of the whole number function, or vice versa, because they have the same name. Ambiguous parameters such as these should be avoided if possible, and if they cannot be, great care should be taken to ensure that the proper overloaded function is called at the proper times.

Function overloading is useful for creating related functions with varied input types, and it gives code a lot of flexibility. One final consideration on overloaded functions is that adequate commenting is essential. Although it might be clear by differing input parameters what version of an overloaded function is right for a particular situation, it might not always be clear precisely what the function does differently from its other overloaded partners.

Discuss this Article

Post your comments
Login:
Forgot password?
Register:
    • In object-oriented computer programming, when two or more functions share the same name but have different parameters it is called function overloading.
      By: il-fede
      In object-oriented computer programming, when two or more functions share the same name but have different parameters it is called function overloading.