Function Overloading

Function overloading refers to the capability of defining several methods that share the same name yet differ in their parameter types and return types. Nevertheless, it is possible for them to have an identical count of parameters. This concept is also referred to as method overloading.

The Function/Method overloading is allowed when:

  • The function name is the same
  • The number of parameters is different in each overloaded function.
  • The number of parameters is the same, and their type is different.
  • The all overloads function must have the same return type.

Imagine we need to carry out multiplication with numbers that have varying counts of parameters. We can define two functions: mula(number, number) for handling two parameters and mulb(number, number, number) for three parameters. However, this distinction in method names can lead to confusion for us and other developers regarding their functionalities. In such situations, employing function overloading becomes essential, as it enhances the clarity and readability of the code.

Advantage of function overloading

  • It saves the memory space so that program execution becomes fast.
  • It provides code reusability, which saves time and efforts.
  • It increases the readability of the program.
  • Code maintenance is easy.

Example

Example

//Function with string type parameter
function add(a:string, b:string): string;
//Function with number type parameter
function add(a:number, b:number): number;
//Function Definition
function add(a: any, b:any): any {
    return a + b;
}
//Result
console.log("Addition: " +add("Hello ", "TypeScript Tutorial")); 
console.log("Addition: "+add(30, 20));

In the above example:

  • The first two lines are the function overload declaration . It has two overloads: A Function which accepts a string parameter. A Function which accepts a number parameter.
  • The third line is the function definition , where the data type of the parameters is set to any .
  • The last two statements invoke the overloaded function.
  • A Function which accepts a string parameter.
  • A Function which accepts a number parameter.

Upon compiling the aforementioned TypeScript program, the resulting JavaScript code will be as follows.

Example

//Function Definition
function add(a, b) {
    return a + b;
}
//Result
console.log("Addition: " + add("Hello ", "TypeScript Tutorial"));
console.log("Addition: " + add(30, 20));

Output:

Function overloading in a class

The subsequent example illustrates how method overloading can be utilized within a class.

Example

class A
{
    public foo(s: string): number;
    public foo(n: number): string;
    public foo(arg: any): any 
    {
        if (typeof(arg) === 'number')
            return arg.toString();
        if (typeof(arg) === 'string')
            return arg.length;
    }
}
let obj = new A();
console.log("Result: " +obj.foo(101));
console.log("Length of String: " +obj.foo("TypeScript Tutorial"));

Upon compiling the aforementioned TypeScript program, the resulting JavaScript code will be as follows.

Example

class A {
    foo(arg) {
        if (typeof (arg) === 'number')
            return arg.toString();
        if (typeof (arg) === 'string')
            return arg.length;
    }
}
let obj = new A();
console.log("Result: " + obj.foo(101));
console.log("Length of String: " + obj.foo("TypeScript Tutorial"));

Output:

Function overloading that involves varying the number of parameters or utilizing different data types while retaining the same function name is not permitted.

Example

Example

function display(x:number, y:number):void //Compiler Error: Duplicate function implementation
{
    console.log(x + x);
}

function display(x:string): void //Compiler Error: Duplicate function implementation
{
    console.log(x);
}

Input Required

This code uses input(). Please provide values below: