Syntax to create function overloading in TypeScript
Function Overloading: Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters and also the data types of all the parameters in different functions would be different as well . When a function name is overloaded with different jobs/ requirements it is thus referred as Function Overloading or Method Overloading.
function function_name(parameter1 : data_type, parameter2: data_type) : function_return_type;
In the above syntax, different functions could be having different parameters of different data types, but constraint here is that the parameters count should be the same in each and every overloaded function, and the name of each and every overloaded functions should be the same.
Example: In this example we will create three functions, first two functions will be just for declarations having different return types, also with different data types of parameters and third function will be solely responsible for executing result in co-relation with first two functions.
Geeksfor Geeks 70
In the above example code, we declare function addFun() with string type of parameters and overload that function as a declaration of another second function with the same name with number type parameters and the last function should have function implementation and what type of value this function will return will decide on what values we are passing as a parameter to this function via the function call.
In the first function call, addFun() with string type arguments so the function will return string. In second function call, we pass addFun() number type arguments so function will return number. Thus, in order to perform function overloading, we should take care of all these above mention things.
Please Login to comment...