Tsallaka zuwa abun ciki
Justice Tech School

Function Overloading

Same name with different parameter types.

Example
#include <iostream>
#include <string>

std::string show(int x) { return "int " + std::to_string(x); }
std::string show(double x) { return "double " + std::to_string(x).substr(0, 4); }
std::string show(const char *x) { return std::string("str ") + x; }

int main() {
  std::cout << show(5) << " " << show(2.5) << " " << show("hi") << "\n";
  return 0;
}

Result

int 5 double 2.50 str hi

Exercise

Same name different types.

Overloads differ by 

Quiz Test

Can return type alone overload?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.