What is Function Overloading?

Article Details
  • Written By: Jessica Susan Reuter
  • Edited By: A. Joseph
  • Last Modified Date: 13 January 2017
  • Copyright Protected:
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog


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.

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.


You might also Like


Discuss this Article

Post your comments

Post Anonymously


forgot password?