
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Function overloading and return type in C++
You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.
The function overloading is basically the compile time polymorphism. It checks the function signature. If the signatures are not same, then they can be overloaded. The return type of a function does not create any effect on function overloading. Same function signature with different return type will not be overloaded.
Following is the example where same function print() is being used to print different data types
Example Code
#include <iostream> using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; pd.print(5); // Call print to print integer pd.print(500.263); // Call print to print float pd.print("Hello C++"); // Call print to print character return 0; }
Output
Printing int: 5 Printing float: 500.263 Printing character: Hello C++
- Related Articles
- method overloading and type promotion in Java
- Function Overloading and Overriding in PHP
- Function overloading and const keyword in C++
- Difference Between Function Overloading and Overriding in C++
- Why is method overloading not possible by changing the return type of the method only in java?
- What is function overloading in JavaScript?
- Return type of getchar(), fgetc() and getc() in C
- What are the best practices for function overloading in JavaScript?
- Implicit return type int in C
- Covariant return type in Java\n
- Importance of return type in Java?
- C function argument and return values
- What is covariant return type in Java?
- Implementing Math function and return m^n in JavaScript
- Method Overloading and null error in Java

Advertisements