6.4. Overloaded Functions

Time: 00:02:46 | Download: Large, Large (CC), Small | Streaming, Streaming (CC) | Slides (PDF)

C++ allows programmers to overload functions by declaring two or more of them with the same name in the same scope. However, functions declared in different scopes (e.g., in different classes or namespaces - detailed in Chapter 9) are not considered overloaded. The only other requirement for successfully overloading functions is that each must have a unique parameter list. The compiler does not consider function return types for overloading; they may be the same or different.

The programming examples presented at the end of Chapter 5 included two similar programs, Time and American, using structures with the same names. Furthermore, both programs have a print function. Imagine that a programmer writes a client program using both structures. How does the compiler "know" which print function we mean when we call it? The older C programming language resolved the problem by requiring the two functions to have different names. C programmers gave the functions similar names because they perform similar tasks: printing a specific structure's fields:print_time and print_american. C++ offers a more elegant solution.

Overloaded Function Examples

C++'s' solution, even in the absence of object-oriented features, is overloading the print function, requiring each to have a unique parameter list.

void print(Time t)
{
	. . . .
}
void print(American am)
{
	. . . .
}
Time		t;
American	a;
	.
	.
	.
	.
print(t);	// the Time print function
print(a);	// the American print function
Overloaded functions: different parameter types. Although both functions have the same name, the compiler can distinguish the calls based on their argument lists.
void make_time(int h, int min, int s)
{
	. . . .
}
void make_time(int s)
{
	. . . .
}
Overloaded functions: different number of parameters. The compiler can easily distinguish between overloaded functions with a different number of parameters.
int function(double x, double y, double z, Time t, int a, int b, int c);
int function(double x, double y, double z, American a, int a, int b, int c);
Overloaded functions: one unique parameter. Programmers can successfully overload functions with many parameters if at least one pair of corresponding function parameters differs.

Invalid Function Overloads

int function(double x);
double function(double x);	// error - incorrect overload
Unable to overload on return types. Overloaded functions may have the same or different return types. However, the compiler cannot distinguish between two functions based only on their respective return types - different return types alone are insufficient for a successful overload.
By Value By Reference
void print(Time t){ ... }
void print(Time& t){ ... }
(a)
print(now);
print(now);
(b)
Unable to overload on pass-by-value vs. pass-by-reference. C++ cannot overload functions based solely on pass-by-value versus pass-by-reference. The examples assume now is an instance of the Time structure.
  1. The ampersand character, &, is only distinguishing difference between the function definitions.
  2. The function calls to pass-by-value and pass-by-reference functions are identical.