Difference between revisions of "Function overloading"

From Tux
Jump to: navigation, search
(Created page with "'''Function overloading''' is a form of polymorphism (identical symbols doing different things in context) in which multiple functions with the same name can be written, subje...")
 
 
Line 15: Line 15:
 
int fcn(int, int);
 
int fcn(int, int);
 
double fcn (int, int);
 
double fcn (int, int);
 +
void fcn(int, int);
 
</source>
 
</source>

Latest revision as of 18:59, 23 January 2018

Function overloading is a form of polymorphism (identical symbols doing different things in context) in which multiple functions with the same name can be written, subject to certain restrictions. The primary restriction is that functions can be overloaded only if there is not a matching function that takes the same number of and type of parameters.

// Given the following function (stated as a prototype)
int fcn(int, int);

// The following functions can be written in the same program:
int fcn(int);
int fcn(int, int, int);
int fcn();
int fcn(double, int);
int fcn(int, double);

// The following functions cannot be written in the same program:
int fcn(int, int);
double fcn (int, int);
void fcn(int, int);