C++ Tutorials


Function Overloading

Tutorials > C++ > Function Overloading

View Full Source

What is Overloading?

Overloading is essentially using the same name for multiple functions or methods. How does the compiler know how to differentiate between them? Each overloaded function must have a different signature. The signature consists of the return value, function name and the parameters. If we change the types or number of paramters of a function but keep the name the same, we create an overloaded function.

Contents of main.cpp :

#include <iostream>
#include <stdlib.h>

using namespace std;

First we create a function called decreasingNumbers that takes 2 parameters. The first parameter is the number to start from. The function will print out all values from this start value down to the value given by the second parameter.

void decreasingNumbers(int from, int to)
	for (int i = from; i >= to; i--)
		cout << i << endl;

We create another function called decreasingNumbers but this time we only have one parameter indicating what value to count from. This function counts all the way down to 0. Instead of using similar code to the code above, we just call the function above and pass 0 as the second parameter.

void decreasingNumbers(int from)
	decreasingNumbers(from, 0);

Below we use both functions. The first prints all values from 10 to 5 and the other prints all values from 7 to 0.

int main()
	decreasingNumbers(10, 5);

	cout << "-----------------" << endl;



	return 0;

You should now have an understanding of what function overloading is. This is very useful in certain situations where you want to create a function that has the same purpose but requires different inputs.

Please let me know of any comments you may have : Contact Me

Source Files : Visual Studio Dev-C++ Unix / Linux

< Tutorial 35 - Random Numbers Tutorial 37 - Writing Text Files >

Back to Top

All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer