Jump to content

Computer Programming/Function overloading

From Wikibooks, open books for an open world

This Computer programming article is available in pseudocode and Ada.

Description

[edit | edit source]

Function overloading (also method overloading) is a programming concept that allows programmers to define two or more functions with the same name and in the same scope.

Each function has a unique signature (or header), which is derived from:

  1. function/procedure name
  2. number of arguments
  3. arguments' type
  4. arguments' order
  5. arguments' name
  6. return type

Please note: Not all above signature options are available in all programming languages.

Available functions overloadings
Language 1 2 3 4 5 6
Ada yes yes yes yes yes yes
C++ yes yes yes yes no no
Java yes yes yes yes no no
Swift yes yes yes yes yes yes

Warning: Function overloading is often confused with function overriding. In Function overloading, a function with a different signature is created, adding to the pool of available functions. In function overriding, however, a function with the same signature is declared, replacing the old function in the context of the new function.

Demonstration

[edit | edit source]

Since functions' names are in this case the same, we must preserve uniqueness of signatures, by changing something from the parameter list (last three alienees).

If the functions' signatures are sufficiently different, the compiler can distinguish which function was intended to be used at each occurrence. This process of searching for the appropriate function is called function resolution and can be quite an intensive one, especially if there are a lot of equally named functions.

Programming languages supporting implicit type conventions usually use promotion of arguments (i.e. type casting of integer to floating-point) when there is no exact function match. The demotion of arguments is rarely used.

When two or more functions match the criteria in function resolution process, an ambiguity error is reported by compiler. Adding more information for the compiler by editing the source code (using for example type casting), can address such doubts.

The example code shows how function overloading can be used. As functions do practically the same thing, it makes sense to use function overloading.

 function int generateNumber(int MaxValue) {
   return rand * MaxValue
 }

 function int generateNumber(int MinValue, int MaxValue) {
   return MinValue + rand * (MaxValue - MinValue)
 }

calling the first function

[edit | edit source]

The first code block will generate numbers from 0 up to specified parameter MaxValue. The appropriate function call is:

 int Number := generateNumber(10);

calling the second function

[edit | edit source]

The second requires another parameter MinValue. Function will return numbers above or equals MinValue and lower than MaxValue.

 int Number := generateNumber(6, 10);

A Swift Example

[edit | edit source]
func doIt(a:Int, b:Int) -> Int {
    return a - b
}

func doIt(a:Int, b:Int) -> Double {
    return Double(b - a)
}

let a = 7
let b = 8
var c = a + b
c = doIt(a:a, b:b)
print(c)

In this example, the constants "a" and "b" are inferred to be Integers, and as a result, the variable "c" is also an integer. As such, the compiler will use the overloaded function "doIt" on line 1, because it is the only function which matches the identifier (name) of the function, parameter types and names (labels), and return type.