Function Overloading

When several functions declarations are specified for a single function name in the same scope, the function name is said to be overloaded. C++ distinguishes functions having the same name by their number and tyoe if arguments.

A function name having several definitions that are differentiable by the number or types of their arguments is known as function overloading.

Overloading improves the readibility of a program by reducing namespace pollution. It makes the language extensible. It is used in inheritence and abstraction.

In order to simulate real-world objects in programming function overloading becomes essential. For overload functions, the compiler automatically decides the particular function to be executed. IT not only reduces the code by reducing the number of if-else statemets but also the code execute faster as so many comparisions are eliminated.

A C++ function name can be overloaded, provided the functions with the same name have different signatures. The signatures differ in the number of arguments or in the type of arguments or both. A function is distinguished from other functions not only by its name but also by the number and type of its arguments.

For example, an overloaded function sum() handles different types of data as shown below:

int sum(int a, int b); //prototype1

int sum(int a, int b, int c); //prototype2

double sum(double x, double y); //prototype3

double sum(int p, double q); //prototype4

double sum(double p, int q); //prototype5

Similarly the functions

int insert(int); //prototype1

int insert(float); //prototype2

int insert(int, int); //prototype3

HOME LEARN C++ PREV NEXT