C++: Function overloading

This entry is part 18 of 61 in the series C++

Function overloading is an important concept in Object-oriented programming.

It allows the same function name for number of times and for different intentions. Having functions of the same name is call overloading.

The overloaded function must be different in their argument list and with different data types.

Consider the example below.

#include <iostream>
using namespace std;

int square(int s)
	return (s*s);

float square(float j)
	return (j*j);

int main()

	int a = 12;
	float b = 3.2;

	cout << "The Square of 12 is " << square(a) << "\n";
	cout << "The Square of 3.2 is " << square(b) << "\n";

	return 0;

Here, we have 2 functions with the same name square.

The first function takes in an integer argument and it returns an integer variable.

The second function takes in a floating number as an argument and it returns a variable.

When calling the functions, we need to pass in the right type of argument.square



Series Navigation<< C++: A function that returns a value
C++: Classes and objects >>