C++ Tutorial

C++ Function Overloading

Same name, different parameter lists. The compiler picks the match from the arguments you pass.

One name, several versions

Overloading means you declare more than one function with the same name. Each version must take a different list of parameters: a different type, a different number of arguments, or both. When you call the name, the compiler looks at the arguments and picks the matching version.

That is useful when the work is the same idea — add two values, print a value, compute an area — but the inputs are not the same type. You keep one name in your head instead of addInt andaddDouble.

Different types

These two functions are both named add. One takes two int values. The other takes two double values. The call add(2, 3) matches the first. The calladd(1.5, 2.5) matches the second.

Example

#include <iostream>
using namespace std;

int add(int a, int b) {
  return a + b;
}

double add(double a, double b) {
  return a + b;
}

int main() {
  cout << add(2, 3) << endl;
  cout << add(1.5, 2.5) << endl;
  return 0;
}

Click Try it in C++ under the example. That opens /cpp/try. Change the numbers and compile again.

Different number of parameters

You can also overload by count. area with one argument is a square. area with two arguments is a rectangle. The compiler does not guess; it counts the arguments you passed.

Example

#include <iostream>
using namespace std;

int area(int side) {
  return side * side;
}

int area(int width, int height) {
  return width * height;
}

int main() {
  cout << area(4) << endl;
  cout << area(4, 6) << endl;
  return 0;
}

How the compiler picks

Matching uses the parameter list, not the function body. The list of types and the number of parameters is the signature. Two functions with the same name are overloads only when the signatures differ.

CallMatches
add(2, 3)add(int, int)
add(1.5, 2.5)add(double, double)
area(4)area(int)
area(4, 6)area(int, int)

If two overloads could both work, g++ reports an ambiguous call. Fix it by passing a value whose type is obvious, or by naming a different function.

Return type is not enough

You cannot overload two functions that take the same parameters and differ only in return type. The compiler decides from the arguments at the call site, and a return type is not an argument. This does not compile:

Example

int value();
double value();

Default arguments can also clash with overloads. If greet() and greet(string name = "Ada")both exist, a call with no arguments has two matches. Keep default arguments or overloads, not both for the same call shape.

Keep the meaning the same

Overload when the versions do the same kind of work. Do not reuse a name for unrelated jobs. A reader who seesadd should get a sum, not a file write. Next: a function that calls itself.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.

Physics

Energy of a mass, two ways

Near Earth, PE is m g h. Translational KE is ½ m v². Same name energy, different parameter lists. The compiler picks from the arguments: three doubles versus two.

2 kg at 3 m height is about 58.9 J of PE. 2 kg at 3 m/s is 9 J of KE. The names look alike; the physics is not.

PE = m g h, KE = ½ m v²

Example

#include <iostream>
using namespace std;

double energy(double mass, double g, double height) {
  return mass * g * height;
}

double energy(double mass, double speed) {
  return 0.5 * mass * speed * speed;
}

int main() {
  cout << "PE = " << energy(2.0, 9.81, 3.0) << " J" << endl;
  cout << "KE = " << energy(2.0, 3.0) << " J" << endl;
  return 0;
}

Maths

Add ints or doubles

2 + 3 is 5. 2.5 + 3.5 is 6. Integer add would truncate the doubles if you had only one function. Overloads keep both exact.

Example

#include <iostream>
using namespace std;

int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }

int main() {
  cout << add(2, 3) << endl;
  cout << add(2.5, 3.5) << endl;
  return 0;
}

FAQ: C++ Function Overloading

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ function overloading examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ function overloading in this C++ C++ lesson (C++ Function Overloading).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.