6.4. Overloaded Functions

function, overloading (definition), overloaded functions (definition), parameter list
Time: 00:02:46 | Download: Large, Large (CC), Small | Streaming, Streaming (CC) | Slides: PDF, PPTX

Overloading means that a programmer can reuse a name or symbol that already has a defined meaning in the current scope, giving it a new meaning. 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 that use similar structures: Time and American. Furthermore, the header files for each structure have a prototype for a print function. Imagine a programmer writing a client program that uses both structures. How does the compiler "know" which print function we mean when we call it? The C programming language resolved the conflict with the requirement that all functions have unique names. When the functions performed similar tasks but with different data, C programmers often named them similarly. For example: print_time and print_american. C++ offers a more elegant solution.

Overloaded Function Examples

function, overloaded functions, parameter, argument

Function overloading is "more elegant" than and superior to multiple function names in two ways. First, it allows programmers to use a single function name to denote various functions that perform essentially the same tasks but with different data types. Using function overloading, programmers can concisely match a function's name to its purpose. Second, it eliminates the inevitable "clutter" that arises from multiple names. The following examples demonstrate three common function-overloading patterns.

Defining Overloaded Functions Calling Overloaded Functions
void print(Time t)
{
	. . . .
}
void print(American a)
{
	. . . .
}
Time		start;
American	units;
	.
	.
	.
	.
print(start);
print(units);
Overloaded functions: different parameter types. As suggested in the introduction, this example imagines a program that uses both the Time and American structures to demonstrate overloading by using the print function. The first pattern is straightforward: each print function has a single, unique parameter. Although functions have the same name, the compiler can distinguish the calls based on their arguments.

 

Defining Overloaded Functions Calling Overloaded Functions
void make_time(int s)
{
	. . . .
}
void make_time(int h, int min, int s)
{
	. . . .
}
int t;
int x;
int y;
int z;

   . . .
make_time(t);
make_time(x, y, z);
Overloaded functions: different number of parameters. The Time structure defines two versions of the make_time function. While both create and fill a Time object, the client program provides the object data in different ways, demonstrating the second pattern. The compiler easily distinguishes between the overloaded functions based on the number of arguments in the call.

 

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. The prototypes in this example demonstrate the third pattern: successfully overloaded functions may have any number of matching argument pairs if the types of at least one corresponding pair differ.

Invalid Function Overloads

function, overloaded functions, return type, argument, parameter. pass-by-value, pass-by-reference
int	function(double x);
double	function(double x);	// error - incorrect overload
Overloading on return types is not allowed. Overloaded functions may have the same or different return types. However, overloading based solely on different return types is insufficient, as programs can ignore or automatically cast returned values, masking their original types. Consequently, the compiler cannot distinguish between two functions based solely on their respective return types.

 

By Value By Reference
void print(Time t){ ... }
void print(Time& t){ ... }
(a)
print(now);
print(now);
(b)
Overloading on value vs. reference parameters is not allowed. 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 pass-by-value and pass-by-reference function calls are identical.